Skip to main content
rcraighead
Legend
Jun 5, 2019 6:26 PM UTC
Answered

my first "for loop" doesn't work.

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 is closed to new replies. Start a new post to keep the conversation going.
pixxxelschubser
pixxxelschubserCorrect answer
Community Expert
Jun 5, 2019 7:40 PM UTC

loop backwards

3 replies

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
Jun 5, 2019 7:40 PM UTC

loop backwards

Translate
rcraighead
Legend
Jun 5, 2019 8:13 PM UTC

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
Jun 5, 2019 8:29 PM UTC

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

This also should also work:  a> -1

Translate