2

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.

2 Answers 2

2

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

10
  • Sorry for my confusion. Actually, I do want to replace the content of the text layers inside the artboard instead of the text layers of the panel.
    – Ryan Yeung
    Oct 2, 2020 at 9:15
  • One last thing and everything is done. If I want to replace whatever words before the @, for example, eeeeee@abc, all the "eeeee", how to writhe the pattern? Should I writh like this: layer.name = layer.name.replace(/.*@/,'eeeee')
    – Ryan Yeung
    Oct 2, 2020 at 9:51
  • remove the first @ in regex Oct 2, 2020 at 9:54
  • It works great! Thank you.I will try to learn photoshop script for the passion like you :)
    – Ryan Yeung
    Oct 2, 2020 at 9:57
  • Great to hear that! Please don't forget to accept the answer to resolve the question Oct 2, 2020 at 10:11
0

I tried myself with the script below, it does works, but don't know if there is any structure I missed:

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
  layer.textItem.contents = layer.textItem.contents.replace(/@.*@/,''); // replacing the layer name. @.*@ regex pattern will select anything between two @ symbols
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.