The script for deleting empty alpha channels.

Community Beginner ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Hello.
Please help me create a script that removes empty alpha channels.
That is, the ones that have a solid color 0 0 0.

That is, I select the alpha channel and if it is empty 0 0 0 0 by RGB, it is deleted.
If not, nothing happens.


I'm not good at scripting, so I'm asking for help.

Thank you very much.

TOPICS
Actions and scripting

Views

108

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Aug 04, 2022 Aug 04, 2022

Likes

Translate

Translate
Community Expert , Aug 04, 2022 Aug 04, 2022

@alinaj84027284 wrote:

I'm not good at scripting, so I'm asking for help.

Thank you very much.


 

@alinaj84027284 – Here is the code recommended by @jazz-y based on the original code from @c.pfaffenbichler

 

// 2022, use it at your own risk;
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.w
...

Likes

Translate

Translate
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

@jazz-y @Stephen_A_Marsh @c.pfaffenbichler @r-bin @Kukurykus maybe can help you with that task.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied


@alinaj84027284 wrote:

I'm not good at scripting, so I'm asking for help.

Thank you very much.


 

@alinaj84027284 – Here is the code recommended by @jazz-y based on the original code from @c.pfaffenbichler

 

// 2022, use it at your own risk;
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.visible = true) {
            var theHisto = thisChannel.histogram;
            // 0 or black
            if (theHisto[0] == theTotal) {
                thisChannel.visible = false;
                thisChannel.remove();
            } else {
                thisChannel.visible = true;
            }
        }
    }
    app.preferences.rulerUnits = originalRulerUnits;
}

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

Thank you so much!
This is just what is needed.

Is it possible to make the script work not on all the alpha channels, but only on the selected one?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

The following code simply removes selected channels, whether they are black or white:

 

// Remove selected channels
// based on code from c.pfaffenbichler
if (app.documents.length > 0) {
    var doc = activeDocument;
    var chanArray = doc.activeChannels;
    for (var i = 0; i < chanArray.length; i++) {
        chanArray[i].remove();
    }
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

// 2022, use it at your own risk;
if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theChannels = myDocument.activeChannels;
    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.visible = true) {
            var theHisto = thisChannel.histogram;
            // 255 or black
            if (theHisto[255] == theTotal) {
                thisChannel.visible = false;
                thisChannel.remove();
            } else {
                thisChannel.visible = true;
            }
        }
    }
    app.preferences.rulerUnits = originalRulerUnits;
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

@c.pfaffenbichler – I think that is what @alinaj84027284 is looking for... I think that you forgot to change to:

 

// 0 or black
if (theHisto[0] == theTotal)

 

To only remove selected black channels while retaining selected white channels.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

LATEST

Ah, sorry, not paying sufficient attention … 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines