Skip to main content
Inspiring
February 12, 2014
Answered

Detecting a layer by name

  • February 12, 2014
  • 1 reply
  • 626 views

The following code is part of a larger script. I'm trying to detect if the layer "Canons Grid" exists. If it doesn't then create a new layer with that name. This code fails everytime, even if the layer doesn't exist. What am i doing wrong?

var doc = app.activeDocument;

var canonLayer;     

      try {

                  canonLayer = doc.layers.itemByName("Canons Grid");

            }

            catch(e)

            {

                  canonLayer = doc.layers.add({name: "Canons Grid"});

            }

This topic has been closed for replies.
Correct answer

This works for me:

var canonLayer = doc.layers.item("Canons");

if (canonLayer.isValid == false) {

    canonLayer= canonLayer.layers.add({name:"Canons"});

}

Not sure what the difference is, but I'll bet someone here will know.

1 reply

Correct answer
February 12, 2014

This works for me:

var canonLayer = doc.layers.item("Canons");

if (canonLayer.isValid == false) {

    canonLayer= canonLayer.layers.add({name:"Canons"});

}

Not sure what the difference is, but I'll bet someone here will know.

tushardeAuthor
Inspiring
February 12, 2014

Thank you. That worked perfectly.