Copy link to clipboard
Copied
#target illustrator
var doc = app.activeDocument;
var allLayers = doc.layers;
for (var i = 0; i < allLayers.length; i++) {
allLayers.locked = false;
if (allLayers.name == "Hypertext" || allLayers.name == "Taps") {
alert("I found the " + allLayers.name + " layer")
} else {
//alert("Not the right layer")
allLayers.remove();
}
}
Can anyone tell me why my loop is stopping short?
I am trying to delete ALL of the layers in the active document that are NOT named Hypertext or Taps.
There are 28 layers in my document (including the 2 specific layers I want to keep).
After I run the script that I wrote (see above script) it does in fact keep my two desired layers but it also doesn't delete all the other layers. I am left with 12 layers that need to be deleted still.
Anyone see what I am doing wrong?
Windows 7 64bit, CS4, JavaScript
Run the loop in reverse.
(var i = allLayers.length-1; i =>0; i--)
Copy link to clipboard
Copied
Run the loop in reverse.
(var i = allLayers.length-1; i =>0; i--)
Copy link to clipboard
Copied
Thanks Larry! That worked out perfect. One small edit to your code was I had to change
(var i = allLayers.length-1; i => 0; i--)
to
(var i = allLayers.length-1; i >= 0; i--)
Final code....
#target illustrator
var doc = app.activeDocument;
var allLayers = doc.layers;
for (var i = allLayers.length-1; i>=0; i--){
allLayers.locked = false;
if (allLayers.name == "Hypertext" || allLayers.name == "Taps") {
//alert("I found the " + allLayers.name + " layer")
} else {
//alert("Not the right layer")
allLayers.remove();
}
}
Thanks again Larry!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now