Background: I'm writing a script to clean up all non-visible layers in a photoshop document. Photoshop layers can be grouped and groups can be nested similar to a directory tree so I'm using recursion.
The problem I'm having is there is there is no foreach function in JavaScript to iterate through each layer in a set. I used for(i=0; i<layers.length; i++)
etc but each time I remove a layer it changes the index of the layers and eventually fails. I fixed this by pushing each layer to a new array and removing them with another for loop. I'm having the same problem with removing empty groups.
Anyway, I have a *semi-working function but it seems like a mess. I tried using for-in loop but it loops through properties like .length and .parent instead of the actual layers. Is there a foreach alternative that would work in this situation? Do I need to run two separate recursive functions to remove the layers then to remove empty groups? Thanks!
Here is the semi-working script.
If you're unfamiliar with running scripts in photoshop just select File>Scripts>Browse and select the .jsx file.