Skip to main content
Known Participant
January 24, 2022
Answered

Javascript to check particular layer is available in the Artwork or not

  • January 24, 2022
  • 1 reply
  • 366 views

Hello Everyone,

 

Currently, I am using the script to hide the Layer "TB"  and lock all layers in the active document:

var document = app.activeDocument;

document.activeLayer = document.layers["TB"]
document.activeLayer.visible = false;

var lc = document.layers.length;
for (var i=1; i<=lc; i++) {
document.layers[i-1].locked = true;
}

 

But in some cases, my document doesnot have the "TB " layer, in that case i need to skip this step and need to run further. 

 

Please give your inputs to overcome this.

This topic has been closed for replies.
Correct answer Charu Rajput

Hi,

You can use, try and catch.

var document = app.activeDocument;
try {
    document.activeLayer = document.layers["TB"];
    document.activeLayer.visible = false;
} catch (e) {

}
var lc = document.layers.length;
for (var i = 1; i <= lc; i++) {
    document.layers[i - 1].locked = true;
}

1 reply

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
January 24, 2022

Hi,

You can use, try and catch.

var document = app.activeDocument;
try {
    document.activeLayer = document.layers["TB"];
    document.activeLayer.visible = false;
} catch (e) {

}
var lc = document.layers.length;
for (var i = 1; i <= lc; i++) {
    document.layers[i - 1].locked = true;
}
Best regards
Surya24Author
Known Participant
January 24, 2022

Thank you so much @Charu Rajput