Delete single alpha channel in PS

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Tony

Delete single alpha channel in PS

Post by Tony »

I'm looking for a script that will delete a single alpha channel from the Channels panel in Photoshop. I want to incorporate it into a Configurator 3 panel.

Ideally, it wouldn't delete color channels and layer mask channels.

I know there are scripts for deleting all non-color, non-layer mask channels, but they run slowly on large images with multiple layers. I'm wondering if a script to delete one alpha channel at a time might be faster, and I can just click the Configurator button with the embedded script repeatedly until I've cleaned up my Channels panel.

Will also ask about the best place to learn how to program java script for PS. Seems have a lot of utility, but I can't figure out how to write the code?

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Delete single alpha channel in PS

Post by Mike Hale »

Unlike working with layers, I wouldn't think the number of layers in a document would affect the speed when working with channels. It could be slow because it is making an unnecessary number of loops through the channels collection. Can you post the code you think is slow?

And even if deleting all the extra channels in one pass is slow in a script I don't see how running a script that deletes one channel multiple times would be any faster. In fact I would expect it to be slower because it would have to loop thorough the channel each run until it found one to delete. That and the time delay of the user clicking on the button between runs. Unless you expect the user to select the channel to delete before running the script. In which case the user could just delete the channel faster than running a script.

Most of the online tutorials and video courses about scripting Photoshop are old. I really think the best place to learn how to script Photoshop is sitting in front of your computer trying to script some task you want done. A editor/debugger named ExtendScript Toolkit is already installed. The scripting guide is either already installed or can be downloaded as a pdf. If you get stuck you can ask for help here or at the Adobe Photoshop scripting forum.
Tony

Delete single alpha channel in PS

Post by Tony »

The current script I'm using is as follows:
Code: Select alldocRef = app.activeDocument;
var i=3;
while(docRef.channels.length>3)

{
   docRef.channels.remove();
}



It works, just slowly on big files with multiple layers. Takes nearly a minute to clear the Channels panel of 5 channel masks when the image file isn't flattened. Once the image file is flattened, it's much quicker.

I was hoping that removing one channel at a time using a script-embedded button on a Configurator panel would speed things up when working with the panel.
Mike Hale

Delete single alpha channel in PS

Post by Mike Hale »

I will bet this is much faster. Note this has not been tested with older version of Photoshop
Code: Select allvar numberOfAlphaChannels = getProperty( charIDToTypeID("Dcmn"), stringIDToTypeID('numberOfChannels') );
for( var channelIndex = numberOfAlphaChannels; channelIndex>1;channelIndex--){
    if(isAlphaChannel(channelIndex)) deleteChannelByIndex(channelIndex);
}

function getProperty( psClass, psKey ){// integer:Class, integer:key
   var ref = new ActionReference();
   if( psKey != undefined ) ref.putProperty( charIDToTypeID( "Prpr" ), psKey );
    ref.putEnumerated( psClass , charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    var desc = executeActionGet(ref);
    var dataType = desc.getType(psKey);
    switch(dataType){// not all types supported - returns undefined if not supported
        case DescValueType.INTEGERTYPE:
            return desc.getInteger(psKey);
            break;
        case DescValueType.ALIASTYPE:
            return desc.getPath(psKey);
            break;
        case DescValueType.BOOLEANTYPE:
            return desc.getBoolean(psKey);
            break;
        case DescValueType.BOOLEANTYPE:
            return desc.getBoolean(psKey);
            break;
        case DescValueType.UNITDOUBLE:
            return desc.getUnitDoubleValue(psKey);
            break;
        case DescValueType.STRINGTYPE:
            return desc.getString(psKey);
            break;
        case  DescValueType.OBJECTTYPE:
            return desc.getObjectValue(psKey);
            break;
    }
};
function isAlphaChannel(index){
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID("Chnl"), index );
    return executeActionGet(ref).hasKey(charIDToTypeID("AChn"));
};
function deleteChannelByIndex( idx ){
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIndex(charIDToTypeID( "Chnl" ), idx )
    desc.putReference( charIDToTypeID( "null" ), ref );
    executeAction( charIDToTypeID( "Dlt " ), desc, DialogModes.NO );
};