Search code examples
javascriptphotoshopextendscriptphotoshop-script

Javascript Photoshop delete paths one by one bug?


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?


Solution

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