skip to Main Content

I am trying to write a script for Photoshop to find if a certain layer exists in the document and then if it does, to rename it. I could do it in Python but am quite confused by the scripting used by Photoshop. So, in Python it would look something like this:

def myfunction (psd):
   for (layer in psd):
       if ( layer == “layer_name”):
           layer = “new_layer_name”

How can this be done in Photoshop?

2

Answers


  1. It’s quite simple provided you don’t have layer groups – and then it get’s a bit more complicated.

    So, you’ve got your psd with five or so layers in, one of them is called "spoon" by accident. This will rename it from "spoon" "my layer name".

    // call the source document
    var srcDoc = app.activeDocument;
    
    renameLayer(srcDoc);
    
    function renameLayer(psd)
    {
      var numOfLayers = psd.layers.length;
    
      // main loop
      for (var i = numOfLayers -2; i >= 0; i--) // ignore background
      {
        var thisLayer = psd.layers[i];
    
        if (thisLayer.name == "spoon")
        {
          // do stuff!
          psd.activeLayer = psd.artLayers[i];
          var rename = "my layer name";
          psd.activeLayer.name = rename;
        }
      }
    }
    

    However, if you have groups then you’ll need to loop over all the layers with something like this.

    // group layer vegetables
    var allLayers = new Array();
    var theLayers = collectAllLayers(app.activeDocument, 0);
    
    
    // function collect all layers
    function collectAllLayers (theParent, level)
    {
       for (var m = theParent.layers.length - 1; m >= 0; m--)
       {
          var theLayer = theParent.layers[m];
          
          // apply the function to layersets;
          if (theLayer.typename == "ArtLayer")
    
          {
             // find the art layers
             doStuff(theLayer)
          }
          else
          {
             allLayers.push(level + theLayer.name);
             collectAllLayers(theLayer, level + 1)
          }
       }
    }
    
    Login or Signup to reply.
  2. Most of Photoshop’s document object collections include the built-in method getByName(String name) for querying the collection by name (see full list here). The method performs a case-sensitive search, returning the first object with a matching name property, or throwing if none found.

    While in this case a simple recursive function traversing across artLayers and into layerSets is most effective, one could also (ab)use the getByName method to perform recursive renaming…

    function renameByName(col, find, replace) {
        for (var i = 0; i < col.layerSets.length; i++)
            renameByName(col.layerSets[i], find, replace);
    
        try {
            while (col.artLayers.getByName(find).name = replace);
        } catch (e) { }
    }
    
    // Use like...
    renameByName(app.activeDocument, "Stelio", "Stelio Was Here");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search