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.
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
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
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;
}
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!!
Copy link to clipboard
Copied
you're welcome jn,
I know you do ThinkingThings