skip to Main Content

I have a Photoshop PSD document which has a layerset named “Flags“. “Flags” has a sub-layerset called “LEFT-Flags“ which itself contains artlayers, each with a national flag image. The number of these artlayers is currently around 40 – but will increase over time.

As an ExtendScript newbie, may I ask how – in ExtendScript – can I extract the NAMES of each of the artlayers to an array please?

2

Answers


  1. Basically it can be done this way:

    var layers = app.activeDocument
        .layerSets['Flags']
        .layerSets['LEFT-Flags']
        .artLayers;
    
    var names = [];
    for (var i=0; i<layers.length; i++) names.push(layers[i].name);
    
    // put the names into a txt file and open the file
    var f = File(Folder.temp + '/layers_names.txt');
    f.open('w');
    f.encoding = 'UTF-8';
    f.write(names.join('n'));
    f.close();
    f.execute();
    

    The array names will contain the names.

    But I don’t know what you want to do with this array. Do you mean to save it as a TXT file?

    Login or Signup to reply.
  2. Scripting with sub-layers is often a pain. One of the best things to do is loop over the whole Photoshop file – which can get slow on how may layers you have. – You might find it useful:

    // group layer vegetables
    var allLayers = new Array();
    var theLayers = collectAllLayers(app.activeDocument, 0);
    
    function doStuff(alayer)
    {
       var currentLayerName = alayer.name;
       alert(currentLayerName);
    }
    
    
    // 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.
Please signup or login to give your own answer.
Back To Top
Search