4
votes

I'm trying to write a Photoshop script that will show all layers of a given name. I need to loop through all the possible nested layer sets and am using the following code:

function showBounds(layerNode)
{
    for(var layer in layerNode.artLayers)
    {
        if (layer.name == "@bounds")
        {
            layer.visible = 1;
        }
    }

    showBounds(layerNode.layerSets);
}

showBounds(app.activeDocument.doc.layerSets);

But when I run it, I get the following error:

Error 1302: No such element
Line: 5
->      for(var layer in layerNode.artLayers)

artLayers should be a property of LayerSets, so I'm confused.

This is also my first attempt at scripting PS (and using javascript), so there might be some fundamental concept I am not getting.

1
You don't need the var in the for(..in..) statementSomeKittens
It looks to me like some layerNode doesn't have a layerNode.artLayers.jfriend00

1 Answers

7
votes

I think you need something more like:

function showBounds(layerNode) {    
    for (var i=0; i<layerNode.length; i++) {

        showBounds(layerNode[i].layerSets);

        for(var layerIndex=0; layerIndex < layerNode[i].artLayers.length; layerIndex++) {
            var layer=layerNode[i].artLayers[layerIndex];
            if (layer.name == "@bounds") {
                layer.visible = 1;
            }
        }
    }
}

showBounds(app.activeDocument.layerSets);

Also, javascripts for...in syntax doesn't work the way you think it does. It's not like a foreach loop. It loops over the property names of an object.