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

Layer loop stops short?

Engaged ,
Oct 16, 2014 Oct 16, 2014

#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

TOPICS
Scripting
426
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 , Oct 16, 2014 Oct 16, 2014

Run the loop in reverse.

(var i = allLayers.length-1; i =>0; i--)

Translate
Adobe
Community Expert ,
Oct 16, 2014 Oct 16, 2014

Run the loop in reverse.

(var i = allLayers.length-1; i =>0; i--)

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
Engaged ,
Oct 16, 2014 Oct 16, 2014
LATEST

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!

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