Skip to main content
rcraighead
Legend
June 5, 2019
해결됨

my first "for loop" doesn't work.

  • June 5, 2019
  • 1 답변
  • 552 조회

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();

     }

  }

이 주제는 답변이 닫혔습니다.
최고의 답변: pixxxelschubser

loop backwards

1 답변

pixxxelschubser
Community Expert
Community Expert
June 5, 2019

loop backwards

rcraighead
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