Vigor
Vigor

Reputation: 1754

Photoshop Javascript to get all layers in the active document

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.

Upvotes: 5

Views: 10673

Answers (4)

Tallulah Yang
Tallulah Yang

Reputation: 11

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);
}

Upvotes: 1

Lex Wills
Lex Wills

Reputation: 29

Minor expansion on Ghoul Fool's post to only get all VISIBLE art layers in the active document. :P

// 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;
}

Upvotes: 1

Javier Aroche
Javier Aroche

Reputation: 71

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;
}

Upvotes: 7

Ghoul Fool
Ghoul Fool

Reputation: 6949

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)
    }
  }
}

Upvotes: 0

Related Questions