Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Sep 28, 2018 Sep 28, 2018

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?

TOPICS
Scripting
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 30, 2018 Sep 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

Translate
Community Expert ,
Sep 28, 2018 Sep 28, 2018

[ moved from InDesign  to InDesign Scripting  ]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 28, 2018 Sep 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();};

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2018 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2018 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 01, 2018 Oct 01, 2018

thanks! this is working perfectly! Much appreciated.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2018 Sep 30, 2018

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

Regards

Malcolm

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 01, 2018 Oct 01, 2018
LATEST

thanks everyone for the help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines