Skip to main content
rcraighead
Legend
June 5, 2019
Answered

my first "for loop" doesn't work.

  • June 5, 2019
  • 1 reply
  • 552 views

Why doesn't it? It deletes all but one hidden item on the targeted layer then errors here: "if (bkItem.hidden == true){" with "no such element"'.

var aDoc = app.activeDocument;

var bkItem = aDoc.layers["BACK TEXT"].pageItems;

var bkCnt = bkItem.length;//# of items in "BACK TEXT"

var a;//counter

for (a=0; a<bkCnt; a++){

    if (bkItem.hidden == true){

        bkItem.remove();

     }

  }

This topic has been closed for replies.
Correct answer pixxxelschubser

loop backwards

1 reply

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
June 5, 2019

loop backwards

rcraighead
Legend
June 5, 2019

Thanks, pixxxel schubser

This worked for me. The other key was adding a "=" to the if statement (a = bkCnt - 1; a >= 0; a--).

var aDoc = app.activeDocument;

var bkItem = aDoc.layers["BACK TEXT"].pageItems;

var bkCnt = bkItem.length;//# of items in "BACK TEXT"

var a;//counter

for (a = bkCnt - 1; a >= 0; a--){

    if(bkItem.hidden == true){

        bkItem.locked = false;

        bkItem.remove();

     }

}

pixxxelschubser
Community Expert
Community Expert
June 5, 2019

Yes, because of pageItems[0] is the first pageItem in the "object array".

This also should also work:  a> -1