Skip to main content
Known Participant
November 8, 2023
Answered

Auto delete the empty layers in the channels? ( Photoshop )

  • November 8, 2023
  • 1 reply
  • 201 views

Can you help me with the scripts that auto delete the empty layers in the channels?

This topic has been closed for replies.
Correct answer Stephen Marsh

@Thihasoe 

 

Full credit to @c.pfaffenbichler for the base code, all I did was wrap around extra code to create a single history step and the option to remove all white alpha channels (rather than black) by holding down ALT/OPT when executing the script.

 

/*
Remove All Black Alphas or ALT-OPT for All White Alphas.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/auto-delete-the-empty-layers-in-the-channels-photoshop/td-p/14221173
Based on
https://community.adobe.com/t5/photoshop-ecosystem-discussions/check-if-channel-have-pixels-or-not-script-code/td-p/13061347
by c.pfaffenbichler
*/

#target photoshop

function main() {

    /* Hold down the ALT/OPT key to remove white alpha channels */
    if (ScriptUI.environment.keyboardState.altKey) {
        if (app.documents.length > 0) {
            var myDocument = app.activeDocument;
            var theChannels = myDocument.channels;
            var originalRulerUnits = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.PIXELS;
            var theTotal = Number(myDocument.width) * Number(myDocument.height);
            for (var m = theChannels.length - 1; m >= 0; m--) {
                var thisChannel = theChannels[m];
                if (thisChannel.kind == "ChannelType.MASKEDAREA") {
                    thisChannel.visible = true;
                    var theHisto = thisChannel.histogram;
                    // black or white
                    if (theHisto[255] == theTotal) {
                        thisChannel.visible = false;
                        thisChannel.remove()
                    } else {
                        thisChannel.visible = true
                    }
                }
            }
            app.preferences.rulerUnits = originalRulerUnits;
        }

    /* Remove black alpha channels */
    } else {
        if (app.documents.length > 0) {
            var myDocument = app.activeDocument;
            var theChannels = myDocument.channels;
            var originalRulerUnits = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.PIXELS;
            var theTotal = Number(myDocument.width) * Number(myDocument.height);
            for (var m = theChannels.length - 1; m >= 0; m--) {
                var thisChannel = theChannels[m];
                if (thisChannel.kind == "ChannelType.MASKEDAREA") {
                    thisChannel.visible = true;
                    var theHisto = thisChannel.histogram;
                    // black or white
                    if (theHisto[0] == theTotal) {
                        thisChannel.visible = false;
                        thisChannel.remove()
                    } else {
                        thisChannel.visible = true
                    }
                }
            }
            app.preferences.rulerUnits = originalRulerUnits;
        }
    }

}

app.activeDocument.suspendHistory("Delete All Empty Alpha Channels...", "main()");

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 8, 2023

@Thihasoe 

 

Full credit to @c.pfaffenbichler for the base code, all I did was wrap around extra code to create a single history step and the option to remove all white alpha channels (rather than black) by holding down ALT/OPT when executing the script.

 

/*
Remove All Black Alphas or ALT-OPT for All White Alphas.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/auto-delete-the-empty-layers-in-the-channels-photoshop/td-p/14221173
Based on
https://community.adobe.com/t5/photoshop-ecosystem-discussions/check-if-channel-have-pixels-or-not-script-code/td-p/13061347
by c.pfaffenbichler
*/

#target photoshop

function main() {

    /* Hold down the ALT/OPT key to remove white alpha channels */
    if (ScriptUI.environment.keyboardState.altKey) {
        if (app.documents.length > 0) {
            var myDocument = app.activeDocument;
            var theChannels = myDocument.channels;
            var originalRulerUnits = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.PIXELS;
            var theTotal = Number(myDocument.width) * Number(myDocument.height);
            for (var m = theChannels.length - 1; m >= 0; m--) {
                var thisChannel = theChannels[m];
                if (thisChannel.kind == "ChannelType.MASKEDAREA") {
                    thisChannel.visible = true;
                    var theHisto = thisChannel.histogram;
                    // black or white
                    if (theHisto[255] == theTotal) {
                        thisChannel.visible = false;
                        thisChannel.remove()
                    } else {
                        thisChannel.visible = true
                    }
                }
            }
            app.preferences.rulerUnits = originalRulerUnits;
        }

    /* Remove black alpha channels */
    } else {
        if (app.documents.length > 0) {
            var myDocument = app.activeDocument;
            var theChannels = myDocument.channels;
            var originalRulerUnits = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.PIXELS;
            var theTotal = Number(myDocument.width) * Number(myDocument.height);
            for (var m = theChannels.length - 1; m >= 0; m--) {
                var thisChannel = theChannels[m];
                if (thisChannel.kind == "ChannelType.MASKEDAREA") {
                    thisChannel.visible = true;
                    var theHisto = thisChannel.histogram;
                    // black or white
                    if (theHisto[0] == theTotal) {
                        thisChannel.visible = false;
                        thisChannel.remove()
                    } else {
                        thisChannel.visible = true
                    }
                }
            }
            app.preferences.rulerUnits = originalRulerUnits;
        }
    }

}

app.activeDocument.suspendHistory("Delete All Empty Alpha Channels...", "main()");

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html