skip to Main Content

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


  1. Chosen as BEST ANSWER

    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
    }


  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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search