Find and replace text problem in Photoshop

Issue

In photoshop, let’s say I have a few texts layers with contents like this:

Text layer 1:  1@@text01@@abc
Text layer 2:  2@@text02@@cef
Text layer 3:  3@@text03@@hgi

I would like to replace all the layers texts (the content of each text layer inside the artboard, not the layer panel’s names) starting with the first@ and the end@, that is @@text..@@ to ## so that results will be:

Text layer 1:  1##abc
Text layer 2:  2##cef
Text layer 3:  3##hgi

Hoe can I achieve this?

Thank you.

Solution

You need to do basically 3 operations:

  • traverse through the layers and select them one by one;
  • replace a part of the layer name using a regular expression pattern;
  • rename the layer with the new name;

A basic* version of this could look like this:

var layer;

// looping through top layers
for (var i = 0; i < activeDocument.layers.length; i++) {
  layer = activeDocument.layers[i]; // for ease of read
  activeDocument.activeLayer = layer; // making the layer active
  layer.name = layer.name.replace(/@.*@/,'##'); // replacing the layer name. @.*@ regex pattern will select anything between two @ symbols
}

Update: replacing the layer text contents.

Basically the same thing with some additions. You need to check if layer.kind is LayerKind.TEXT and instead of changing layer.name you need to change layer.textItem.contents

*. note that activeDocument.layers collection contains only top level layers. If your document has groups (aka folders) or artboards you’ll need to go through nested layers with a different function: something like this

Answered By – Sergey Kritskiy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published