Search code examples
javascriptadobephotoshopextendscript

Photoshop javascript - Removing a channel by text contained in the name


I have a problem with the photoshop channels.
I'm trying to remove a channel if part of its name matches with "MaskFF".
So it could be "MaskFF", or "MaskFF_12345", or "12345 MaskFF".
But so far I haven't had any luck, everything I use gives me "Error 1302: No such element".
I also tried simple loops like this, to get an alert with the name of each channel:

var doc = activeDocument;
for(i=0; i<doc.channels.length; i++);{
    alert(doc.channels[i].name);
    }

but even that doesn't work. Am I doing something wrong? Any ideas?


EDIT

Seems like there was a semicolon where it shouldn't have been.
Now the loop works.


Solution

  • I found the answer by myself:

        for (i = app.activeDocument.channels.length-1; i >= 0; i--) { 
            if (app.activeDocument.channels[i].name.indexOf("MaskFF") !==-1) {
                app.activeDocument.channels[i].remove ();
            }
        }
    

    or

        for (i = app.activeDocument.channels.length-1; i >= 0; i--) { 
            if (/MaskFF/.test(app.activeDocument.channels[i])) {
                app.activeDocument.channels[i].remove ();
            }
        }