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

indesign cc 2017 - script to turn off/on layer(s) in open documents?

Explorer ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

Hi, I found a code to turn off or on a layer(s) by invoking it in indesign from here.

Short Cut Keys to Make Layers visible in In Design CS 5.5

    app.activeDocument.layers.item("Some Layer Name 1").visible = true;

    app.activeDocument.layers.item("Some Layer Name 2").visible = true;

It works by changing the names of the layers you want to turn off or on and setting the flag to true or false. If the layer doesn't exist, it will ignore it.

But how do i incorporate into a script that will apply to all open documents, instead of single doc? I've been googling and couldn't find similar sample.

Regards

Brian

TOPICS
Scripting

Views

3.3K

Translate

Translate

Report

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

Explorer , Aug 07, 2017 Aug 07, 2017

Hi Peter, thank you for the update!

Final script is here, with grep:

for (var i = app.documents.length-1; i >= 0; i--) { 

  for (var j = app.documents.layers.length-1; j >= 0; j--) { 

    if (/Guides|Dieline/.test (app.documents.layers.name)) { 

      app.documents.layers.visible = false;

    } 

  } 

Votes

Translate

Translate
Participant ,
Aug 02, 2017 Aug 02, 2017

Copy link to clipboard

Copied

Place the code in a loop such as:

for(var i = 0; i<=app.documents.length-1; i++){

     app.documents.layers.item("Some Layer Name 1").visible = true;

}

Maybe I am missing something, but this should do it.

Votes

Translate

Translate

Report

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 ,
Aug 03, 2017 Aug 03, 2017

Copy link to clipboard

Copied

Hi Roy,

Initially, it gave me an error when i pasted the code (illegal character "), but then I retyped the quote and it works!!

Thank you so much for your help. This was nagging me yesterday.

I have a follow up question, if one of the layer is not present, it would generate an error, how to ignore the non-existent layer?

For example, If there is no Dieline layer in the doc, how to tell the script to ignore it and keep running?

error string: object is invalid

app.documents.layers.item("Dieline").visible = false;

for(var i = 0; i<=app.documents.length-1; i++){

     app.documents.layers.item("Dieline").visible = false;

     app.documents.layers.item("Guides").visible = false;

}

Votes

Translate

Translate

Report

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
Guide ,
Aug 03, 2017 Aug 03, 2017

Copy link to clipboard

Copied

use try and catch method..simply.. otherwise use isValid or exists method

Votes

Translate

Translate

Report

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 Beginner ,
Aug 03, 2017 Aug 03, 2017

Copy link to clipboard

Copied

Another alternative would be to cycle through and get all the layer names of a document first, then push them to an array and use those layer names in the script.

Votes

Translate

Translate

Report

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 ,
Aug 03, 2017 Aug 03, 2017

Copy link to clipboard

Copied

I agree the above reply. I would use the Catch option in this case. I have trouble in my own head though when using catch for things like this. It seems like a lazy option, but is very effective! I guess I am lazy.

try{

     app.documents.layers.item("Dieline").visible = false;

}

catch(e){

//skip it

}

Votes

Translate

Translate

Report

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
Guide ,
Aug 03, 2017 Aug 03, 2017

Copy link to clipboard

Copied

but I usually use isValid method..In try catch method we may know the error details in alert

for(var i = 0; i<=app.documents.length-1; i++){

    if(app.documents.layers.item("Dieline").isValid){

     app.documents.layers.item("Dieline").visible = false;

}

else{alert("Dieline layer not present")}

}

Votes

Translate

Translate

Report

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 ,
Aug 04, 2017 Aug 04, 2017

Copy link to clipboard

Copied

Hello all, with my extremely limited knowledge of programming. I managed to make it works with tpk's code. I am sure Roy's way could be as good, it was just not as intuitive to me to incorporate since I am not a programmer.

This is what i came up with and does exactly what I want.

To help somebody else, here it is. I don't need an alert, so i deleted it.

for(var i = 0; i<=app.documents.length-1; i++){

    if(app.documents.layers.item("Guides").isValid){

     app.documents.layers.item("Guides").visible = true;

}

else{}

}

for(var i = 0; i<=app.documents.length-1; i++){

    if(app.documents.layers.item("Dieline").isValid){

     app.documents.layers.item("Dieline").visible = true;

}

else{}

}

It checks to see if these layers exist, turn it on or off on all open window. if the layer doesn't exist, it will skip, no errors prompts or alert.

Substitute the name of the layer with your own, you can add another layer by repeating the code. And you can turn off the layer(s) by changing the visibility from false to true.

I am sure there is a better way to write this.

Regards to all,

Brian

Votes

Translate

Translate

Report

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 Beginner ,
Jul 13, 2020 Jul 13, 2020

Copy link to clipboard

Copied

I've tried running this script on CC 2020, but keep getting this error stating:

 

Javascript Error!

Error Number: 55

Error String: Object does not support the property or method 'layers'

 

The script i'm trying to run is as follows:

 

for(var i = 0; i<=app.documents.length-1; i++){

if(app.documents.layers.item("NN").isValid){

app.documents.layers.item("NN").visible = true;

}

else{}

}

for(var i = 0; i<=app.documents.length-1; i++){

if(app.documents.layers.item("NL").isValid){

app.documents.layers.item("NL").visible = false;

}

else{}

}

 

Any insight as to why this error is occuring perhaps?

Thanks!

Votes

Translate

Translate

Report

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 ,
Aug 04, 2017 Aug 04, 2017

Copy link to clipboard

Copied

And yet another possibility is the following. A bit of GREP is always useful:

for (var i = app.documents.length-1; i >= 0; i--) {

  for (var j = app.documents.layers.length-1; j >= 0; j--) {

    if (/Dieline|Guides/.test (app.documents.layers.name)) {

      app.documents.layers.visible = true;

    }

  }

}

Peter

Votes

Translate

Translate

Report

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 ,
Aug 04, 2017 Aug 04, 2017

Copy link to clipboard

Copied

Hi Peter,

Thank you for your more elegant approach!

However when executed, it didn't apply to all open document and it only turn on the first layer found.

The goal is to have all listed layers to be on/off, not just the first one found.

Regards,

Brian

Votes

Translate

Translate

Report

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 ,
Aug 05, 2017 Aug 05, 2017

Copy link to clipboard

Copied

Sorry, this line:

app.documents.layers[i].visible = true;

should be

app.documents.layers[j].visible = true;

(that is, layers > layers)

P.

Votes

Translate

Translate

Report

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 ,
Aug 07, 2017 Aug 07, 2017

Copy link to clipboard

Copied

Hi Peter, thank you for the update!

Final script is here, with grep:

for (var i = app.documents.length-1; i >= 0; i--) { 

  for (var j = app.documents.layers.length-1; j >= 0; j--) { 

    if (/Guides|Dieline/.test (app.documents.layers.name)) { 

      app.documents.layers.visible = false;

    } 

  } 

Votes

Translate

Translate

Report

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 ,
Aug 07, 2017 Aug 07, 2017

Copy link to clipboard

Copied

I learned something today! Using GREP to test for the text in the layer name. Very useful.

Up to now, I would have done a 2 step indexOf, or search to get the condition

if((app.documents.layers.name.indexOf("Guides")!=-1)||(app.documents.layers.name.indexOf("Dieline")!=-1)){

}

but GREP is far more elegant.

I must try to remember to use this myself!

Votes

Translate

Translate

Report

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 ,
Aug 07, 2017 Aug 07, 2017

Copy link to clipboard

Copied

Grep is useful everywhere where you work with text strings. For instance, if you have a series of paragraph styles for different levels of sections, as in 'Heading 1', 'Heading 2', 'Heading 3', etc., you can check if a given paragraph has a numbered heading style applied using e.g.

if (/Heading \d/.test (myParagraph.appliedParagraphStyle.name)) {

Compact and quick.

Votes

Translate

Translate

Report

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 ,
Jul 13, 2020 Jul 13, 2020

Copy link to clipboard

Copied

Hi Tom,

the code in this thread was damaged by moving the thread from the old InDesign Scripting forum to the new InDesign forum last year. I cannot change Brian's answer that is marked "Correct", but I can post his code rephrased like that:

 

var openDocuments = app.documents.everyItem().getElements();

for( var n=0; n<openDocuments.length; n++ )
{ 
	var currentDoc = openDocuments[n] ;
	var currentLayers = currentDoc.layers.everyItem().getElements();
	
	for ( var j =0; j <currentLayers.length ; j++)
	{ 
		var currentLayer = currentLayers[j];
		if ( /Guides|Dieline/.test ( currentLayer.name ) )
		{ 
			currentLayer.visible = false;
		};

	};

} ;

 

Hope, that helps…

 

Regards,
Uwe Laubender

( ACP )

 

Votes

Translate

Translate

Report

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 Beginner ,
Jul 13, 2020 Jul 13, 2020

Copy link to clipboard

Copied

Hi Uwe,

 

That did the trick! Thanks so much for your help!

Votes

Translate

Translate

Report

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
New Here ,
Apr 06, 2021 Apr 06, 2021

Copy link to clipboard

Copied

Hi I am trying this script in but i got an error message "Error: ReferenceError: Can't find variable: app"
don't know what's the issue.
Regard,

BabyAnne S.

Votes

Translate

Translate

Report

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 ,
Apr 06, 2021 Apr 06, 2021

Copy link to clipboard

Copied

If you are running the script from ExtendScript make sure you target InDesign

 

Screen Shot.png

Votes

Translate

Translate

Report

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
New Here ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

Hi Rob, thank you.. but currently I can't use Extendscript in my mac,, looks like it is not working in a 64bit setup 😞 

Votes

Translate

Translate

Report

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 ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

LATEST

Hello Anne

Please use ExtendScript Debug for Visual Studio

https://marketplace.visualstudio.com/items?itemName=Adobe.extendscript-debug

 

 

Best regards

Votes

Translate

Translate

Report

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