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

script for selecting + activating next layer

New Here ,
Jan 29, 2019 Jan 29, 2019

hi there

first of all: i have absolutely no knowledge in scripting myself.

here’s want to do: say i have 3 layers. i'm working in layer 1, it is visible and active. layer 2 and 3 are hidden and inactive.

now it would be very efficient to have a script to hide/deactivate layer 1, unhide and make active layer 2. then the same for switching from layer 2 to layer 3.

i found this script by Loic Aigon which hides/unhides a layer, but does nothing more than that:

if(app.documents.length>0)

{

  var doc = app.activeDocument;

  l = doc.activeLayer;

  //alert(l.name);

  if(l.visible==true)

  {

  l.visible=false;

  }

  else

  {

  l.visible=true;

  }

  l=null;

}

i found some other similar scripts for photoshop and/or illustrator. i tried using them in indesign, but without any luck whatsoever.

also, these scripts i found seem to use the layer name in the script. it would be great if the script could select and activate the “next”/“previous” layer, independently from the layer name.

any help would be greatly appreciated.

TOPICS
Scripting
4.3K
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 , Jan 29, 2019 Jan 29, 2019

Hi pincms ,

you did not tell if layer 2 ist stacked above layer 1.

I simply assume that.

( function()

{

    if( app.documents.length == 0 ){ return };

   

    var doc = app.documents[0];

    var i = doc.activeLayer.index;

   

    doc.layers.everyItem().visible = false ;

    doc.activeLayer = doc.layers[ i-1 ];

    doc.activeLayer.visible = true;

}() )

Regards,
Uwe

Translate
Community Expert ,
Jan 29, 2019 Jan 29, 2019

Hi pincms ,

you did not tell if layer 2 ist stacked above layer 1.

I simply assume that.

( function()

{

    if( app.documents.length == 0 ){ return };

   

    var doc = app.documents[0];

    var i = doc.activeLayer.index;

   

    doc.layers.everyItem().visible = false ;

    doc.activeLayer = doc.layers[ i-1 ];

    doc.activeLayer.visible = true;

}() )

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
New Here ,
Jan 30, 2019 Jan 30, 2019

thank you uwe!

yes, your assumption was correct.

i got it to work like this in InDesign via Adobe ExtendScript Toolkit:

  1.     if( app.documents.length>0);
  2.     var doc = app.documents[0];
  3.     var i = doc.activeLayer.index;
  4.     doc.layers.everyItem().visible = false ;
  5.     doc.activeLayer = doc.layers[ i-1 ];
  6.     doc.activeLayer.visible = true;

so without lines 1, 2, 12 and 13 from the code you provided.

i don't know why this is, but since it's working, it doesn't bother me at all.

i also figured out how to switch to the previous layer instead of to the next ( [ i+1 ] instead of [ i-1 ] in line 8 from my altered code) and saved it as a separate script. now i have the shortcuts Alt+Page up and Alt+Page down assigned to the two scripts and my productivity will go up like crazy!

thanks again, your help is very 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
New Here ,
Jan 30, 2019 Jan 30, 2019

... just wanted to get to work with the new shortcuts and ran into a problem:

the script hides ALL layers before activating the next/previous. since i have 2 base layers, that's not useful.

here's how i changed uwe's script so that it only hides the currently active layer:

  1.     if( app.documents.length>0);
  2.    
  3.     var doc = app.documents[0];
  4.     var i = doc.activeLayer.index;
  5.    
  6.     doc.activeLayer.visible = false ;
  7.     doc.activeLayer = doc.layers[ i-1 ];
  8.     doc.activeLayer.visible = true;
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 ,
Jan 30, 2019 Jan 30, 2019

Hi pincms ,

did you ask yourself why doc.layers[ i-1 ] is working as a roundtrip going up layer through layer and when the top most is reached going straight to the bottom most layer as next step?

And did you ask yourself why doc.layers[ i+1 ] is going down, but stops at the bottom most layer? And is not working as a roundtrip through the layers? Means that doc.layers[ i-1 ] can cycle more times than once through all the layers and doc.layers[ i+1 ] will stop working after the time the bottom of the layers is reached?

Here some answers:

1. doc.layers is not an array, but a collection.

2. doc.layers[ indexnumber ] is something like a shorthand for layers.item( indexnumber ).

3. Let's say we have three layers in a document from bottom to top named A, B and C.

The top one has always index 0 counting up to the bottom element.

So in order how you see the layers stacked in the Layers panel the corresponding index numbers are:

C has index 0

B has index 1

A has index 2

The speciality is that a layer with index -1 is also present.

You'll be not aware of this, but in that collection of layers this is the last one in the collection. In my example above that is the layer named A. index -1 is the shorthand for the last element in a collection.

If your active layer is layer C with index 0, variable i has the value 0, the next active layer then will be 0 minus 1 which leads to the result that layer A, the last element of the collection is the next one that is activated.

Wheras if you try to cycle through with i+1 you come to an natural end.

If your active layer is A that has index 2 and you add 1 to that number with i+1 you end up with an index number for the next active layer that is not present in the collection. There is no layer with index number 3. Your script will throw an error.

What can be done about this?

Put an additional line of code in the script, a condition that compares the value of i with the length of the collection minus one.

Every collection has a length property and counts the number of elements in that collection. The number of elements here is 3. 3 minus 1 is the last index number in the above collection of layers of this example.

If the condition outlined above is true, then set i to -1.

Why to -1 ? To get to the top of your collection you need index value 0.

And when i is -1, i+1 is actually 0, the top index value, the index of the first element in the collection; in our example here the layer named C.

if( i == doc.layers.length-1 ){ i = -1 };

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
New Here ,
Jan 30, 2019 Jan 30, 2019

Uwe, i'm impressed by your knowledge and understanding of the matter and your support.

i think i can follow your explanation, but certainly would never have even come close to figuring it out myself.

i'm totally happy with the way the i can handle layer switching now, but since you seem to enjoy this sort of problem solving, i have another little challenge for you – a rather easy one for you, i assume:

is there a way to make the script NOT change the visibility of the layer if it is locked (but still cycle through)?

context: i want the visibility of my 2 base layers at the bottom and 2 layers at the top not accidentally be changed.

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 ,
Feb 02, 2019 Feb 02, 2019
LATEST

Look into the DOM ( Document Object Model ) documentation.

There you see that there is another property for the layer object that is: locked.

It is marked as Boolean. Means, it has two possible values, true or false.

Try to do an if statement. If locked is true do this thing, if locked is not true do another thing.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Layer.html

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