Skip to main content
dublove
Legend
June 3, 2026
Question

What is an effective way to detect the existence of a certain layer? There is a function, but it cannot detect sub layers.

  • June 3, 2026
  • 1 reply
  • 24 views

I tried (!layer.isValid) or (layer=false).
Neither worked; they both threw errors saying it was invalid.

Later, I found the function below and was overjoyed.
But then I discovered it couldn’t detect names within nested layers.
I’m asking for help from the experts.

var d=activeDocument;
var ln=”Fix Guides”;
//Check if a layer exists
alert(!layerExists(d, ln));

function layerExists(d, ln) {
    var map = buildLayerMap(d);
    return map.hasOwnProperty(ln);

}

function buildLayerMap(d) {
    var map = {};
    for (var i = 0; i < d.layers.length; i++) {
        map[d.layers[i].name] = d.layers[i];
    }
    return map;
}

 

1 reply

Ton Frederiks
Community Expert
Community Expert
June 3, 2026

@dublove  This is what I was talking about in your other post, add a scripting tag: