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

loop through layers setting lock and visibility

New Here ,
Mar 21, 2015 Mar 21, 2015

Copy link to clipboard

Copied

  I need to loop through the layers in the active document and set some of the layer properties to either false or true.  I was trying to a create a loop using a "for" method, but it didn't seem to work. I gave up on the "for" method because I kept running into a problem.  I don't need to set all layers with these settings just the odd ones (2,4,6, etc). Here is sample if it was written out. 

var myLayer = myDoc.layers[2];

myLayer.visible = false;

myLayer.locked = true;

var myLayer = myDoc.layers[4];

myLayer.visible = false;

myLayer.locked = true;

var myLayer = myDoc.layers[6];

myLayer.visible = false;

myLayer.locked = true;

var myLayer = myDoc.layers[8];

myLayer.visible = false;

myLayer.locked = true;

I have 208 layers, so doing it this way would be way to long. Any help would be appreciated.

Running Illustrator CC 2014.

TOPICS
Scripting

Views

464

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

Engaged , Mar 21, 2015 Mar 21, 2015

After I wrote this and tested it, I saw that Carlos already answered it, but I thought that I would post it anyway. Carlos KNOWS how much I love JS... 😉

One difference is that since it wasn't clear to me which layer you were referring to as layer #1, I started from the bottom of the heap.  Hope this helps.  -TT

var aDoc = app.activeDocument;

var lc = aDoc.layers.length;
for (var i = 2; i <= lc; i+=2) {
    var curLayer = lc - i
    aDoc.layers[curLayer].visible = false;
    aDoc.layers[curLayer].loc

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 21, 2015 Mar 21, 2015

Copy link to clipboard

Copied

why didn't the for loop work? what was the problem you kept running into?

to target every other layer try

for (var a=0; a<myDoc.layers.length; a+=2) // 0, 2, 4, 6

to start with the third layer [2]

for (var a=2; a<myDoc.layers.length; a+=2) // 2, 4, 6, 8

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
Engaged ,
Mar 21, 2015 Mar 21, 2015

Copy link to clipboard

Copied

After I wrote this and tested it, I saw that Carlos already answered it, but I thought that I would post it anyway. Carlos KNOWS how much I love JS... 😉

One difference is that since it wasn't clear to me which layer you were referring to as layer #1, I started from the bottom of the heap.  Hope this helps.  -TT

var aDoc = app.activeDocument;

var lc = aDoc.layers.length;
for (var i = 2; i <= lc; i+=2) {
    var curLayer = lc - i
    aDoc.layers[curLayer].visible = false;
    aDoc.layers[curLayer].locked = true;
}

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 ,
Mar 21, 2015 Mar 21, 2015

Copy link to clipboard

Copied

@ Thinking and Carlos


First of all thanks for responding so quickly.  After looking at your code I think I know why it didn't work. I type this " i + 2"  instead of what both of you have " I+=2". SMH.  Tested it out and they both work.  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 ,
Mar 21, 2015 Mar 21, 2015

Copy link to clipboard

Copied

LATEST

you're welcome jn,

I know you do ThinkingThings

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