skip to Main Content

Photoshop CC 2017. Using this loop to delete paths one by one results in some paths (out of 8) being deleted only:

 for(i = 0; i < app.activeDocument.pathItems.length; i++) {
             alert(i)
             app.activeDocument.pathItems[i].remove();
        }   

The length gets reported as being 8. However alert(i) only shows 4 times. All the paths get removed only if running the loop multiple times. I’m deleting them one by one because I want to keep a path with a certain name. Any ideas?

2

Answers


  1. You are changing pathItems as you loop through it. When you delete item i there will be a new item at position i that you skip.
    If you do the loop backwards it won’t cause any problems

     for(i = app.activeDocument.pathItems.length -1; i >= 0; i--) {
        alert(i)
        app.activeDocument.pathItems[i].remove();
     } 
    
    Login or Signup to reply.
  2. You can use pathItems.removeAll() in this case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search