Skip to main content
Inspiring
September 28, 2018
Answered

Script to hide layer named "Draft" in multiple files.

  • September 28, 2018
  • 4 replies
  • 1813 views

new to scripting... please help! I have this script:

app.activeDocument.save();

var myWaterLayerName = "Draft";

try{app.activeDocument.layers.item(myWaterLayerName).visible = false;}

catch(_){alert("Can't find layer: " + myWaterLayerName);exit();};

that hides a layer in the active document. how can i modify to work on all open documents?

This topic has been closed for replies.
Correct answer Laubender

Hi Malcom,

you need no loop because you can work with the collection of documents:

app.documents.everyItem().layers.itemByName("Draft").visible = false;

This is working even if some of the documents do not contain a layer named "Draft".

getElements() will give you an array. Because of that you have to loop.

Regards,
Uwe

4 replies

Inspiring
October 1, 2018

thanks everyone for the help!

BarlaeDC
Community Expert
September 30, 2018

Of course, don't know how I didn't see that.

Regards

Malcolm

Known Participant
September 28, 2018

Hello!

I am new at scripting as well, but I think I’ve come up with a solution for you. The script may take a while to run if you have a lot of documents open.

Try the code below.

  var myWaterLayerName = "Draft";

        for(myCounter = app.documents.length;

              myCounter > 0;

              myCounter--){

        try{

            app.documents.item(myCounter-1).layers.item(myWaterLayerName).visible = false;

            app.documents.item(myCounter-1).save();         

            }

        catch(_){alert("Can't find layer: " + myWaterLayerName);exit();};

    }

BarlaeDC
Community Expert
September 28, 2018

Hi,

a similar but simpler script to do the job,

var toHideLayers = app.documents.everyItem().layers.itemByName("Draft").getElements();

for ( var i = 0; i < toHideLayers.length; i++)

{

    toHideLayers.visible = false;

}

This will get the layer called "Draft" from every open document.

Hope this helps.

Malcolm

LaubenderCorrect answer
Community Expert
September 30, 2018

Hi Malcom,

you need no loop because you can work with the collection of documents:

app.documents.everyItem().layers.itemByName("Draft").visible = false;

This is working even if some of the documents do not contain a layer named "Draft".

getElements() will give you an array. Because of that you have to loop.

Regards,
Uwe

pixxxelschubser
Community Expert
September 28, 2018

[ moved from InDesign  to InDesign Scripting  ]