skip to Main Content

I’m sure it should be discussed before by Photoshop scripters. I write a solution as following. I think it’s logically right, but the result is not correct. Anybody can help to check where’s wrong in the code, or have ideas for this topic? I want to get all the layers in a document.

Code:

function getAllLayersInLayerSets(layerNodes) {

 var retList = [];

 for (var i=0; i<layerNodes.length; i++) {

    if(layerNodes[i].layerSets.length > 0)
    {
        var tmp = getAllLayersInLayerSets(layerNodes[i].layerSets);

        var j = (tmp == null) ? -1 : tmp.length-1;
        while(tmp && j>=0)
        {
            retList.push(tmp[i]);
            j--;
        }
    }
    for(var layerIndex=0; layerIndex < layerNodes[i].artLayers.length; layerIndex++) 
    {
        var layer=layerNodes[i].artLayers[layerIndex];
        retList.push(layer);
    }

}

return retList;  
}

Many thanks for any help or discussion.

4

Answers


  1. To get all the layers (and sub layers) you have to have a recursive function

    var allLayers = new Array();
    var theLayers = collectAllLayers(app.activeDocument, 0);
    
    
    function collectAllLayers (theParent, level)
    {
      for (var m = theParent.layers.length - 1; m >= 0; m--)
      {
        var theLayer = theParent.layers[m];
        if (theLayer.typename != "ArtLayer")
        {
          allLayers.push(level + theLayer.name);
          collectAllLayers(theLayer, level + 1)
        }
      }
    }
    
    Login or Signup to reply.
  2. I know this is an old thread, but this might be useful for someone.

    I was looking for a function that would get me all the ArtLayers in a Photoshop comp, including layers nested in groups. The above function was returning undefined, so I modified it and got it to work.

    var doc = app.activeDocument;
    var allLayers = [];
    var allLayers = collectAllLayers(doc, allLayers);
    
    function collectAllLayers (doc, allLayers){
        for (var m = 0; m < doc.layers.length; m++){
            var theLayer = doc.layers[m];
            if (theLayer.typename === "ArtLayer"){
                allLayers.push(theLayer);
            }else{
                collectAllLayers(theLayer, allLayers);
            }
        }
        return allLayers;
    }
    
    Login or Signup to reply.
  3. Minor expansion on Ghoul Fool’s post to only get all VISIBLE art layers in the active document. 😛

    // Get layers in a document
    var sourceDocument = app.activeDocument;
    var visibleLayers  = [];
    var visibleLayers  = collectAllLayers(sourceDocument, visibleLayers);
    
    // Print out total layers found
    alert(visibleLayers.length);
    
    
    // Recursively get all visible art layers in a given document
    function collectAllLayers (parent, allLayers)
    {
        for (var m = 0; m < parent.layers.length; m++)
        {
            var currentLayer = parent.layers[m];
            if (currentLayer.typename === "ArtLayer")
            {
                if(currentLayer.visible)
                {
                    allLayers.push(currentLayer);
                }
            }
            else
            {
                collectAllLayers(currentLayer, allLayers);
            }
        }
        return allLayers;
    }
    
    Login or Signup to reply.
  4. function selectAllLayers() {
        var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
        desc29.putReference(charIDToTypeID('null'), ref23);
        executeAction(stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search