Script to select contents of alpha channels..

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

Halcyon

Script to select contents of alpha channels..

Post by Halcyon »

Hello all!

I'm looking for a script that can select the contents of all alpha channels (not RGB channels). Doing this manually - you would select your alpha channel, convert to a selection and then repeat the process for the next alpha channel (while holding down "Shift"). Doing this for a document with 15 alpha channels can be a real pain, and I can't build an action for this.

Can someone help?
Thanks!

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

Mike Hale

Script to select contents of alpha channels..

Post by Mike Hale »

Something like this?
Code: Select allvar numberOfChannels = getNumberOfChannels();
var loadType = hasSelection() ? 'add':'load';
for(var c=0;c<numberOfChannels+1;c++){
    if(isAlphaChannel(c)){
        if(loadType=='load'){
            loadChannelAsSelection(getChannelNameByIndex(c));
            loadType='add';
        }else{
            addChannelToSelection(getChannelNameByIndex(c));
        }
    }
};
function getNumberOfChannels(){   
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   return executeActionGet(ref).getInteger(stringIDToTypeID('numberOfChannels'))// does not count layer masks
};
function isAlphaChannel(index){// integer:AM index - optional
    var desc = getProperty(charIDToTypeID("Chnl"),charIDToTypeID("AChn"), index);
    if(desc != undefined){
        var channelType = desc.getEnumerationValue(charIDToTypeID("ClrI"));
        return channelType == charIDToTypeID('MskA') || channelType == charIDToTypeID('SlcA')? true:false;
    }else{
        return false;
   }
};
function getChannelNameByIndex(amIndex){   
   var ref = new ActionReference();
    ref.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID("ChnN") );
   ref.putIndex( charIDToTypeID("Chnl"), amIndex );
   return executeActionGet(ref).getString(charIDToTypeID("ChnN"));
};
function loadChannelAsSelection(channelName){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var chnlDesc = new ActionReference();
        chnlDesc.putName( charIDToTypeID( "Chnl" ), channelName );
    desc.putReference( charIDToTypeID( "T   " ), chnlDesc );
   executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO )
};
function addChannelToSelection(channelName) {
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putName( charIDToTypeID('Chnl'), channelName );
    desc.putReference( charIDToTypeID('null'), ref );
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
    desc.putReference( charIDToTypeID('T   '), ref );
    executeAction( charIDToTypeID('Add '), desc, DialogModes.NO );
};
function hasSelection(){
   var res = false;
   try{
      app.activeDocument.selection.bounds;
      res = true;
   }catch(e){}
   return res;
};
function getProperty( psClass, psKey, index ){// integer:Class, integer:key
    var ref = new ActionReference();
    if( psKey != undefined ) ref.putProperty( charIDToTypeID( "Prpr" ), psKey );
    if(index != undefined ){
        ref.putIndex( psClass, index );
    }else{
        ref.putEnumerated( psClass , charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    }
    try{
        var desc = executeActionGet(ref);
    }catch(e){ return; }// return on error
    if(desc.count == 0) return;// return undefined if property doesn't exists
    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;
        case  DescValueType.LISTTYPE:
            return desc.getList(psKey);
            break;
        case  DescValueType.ENUMERATEDTYPE:
            return desc.getEnumerationValue(psKey);
            break;
    }
};
Halcyon

Script to select contents of alpha channels..

Post by Halcyon »

Yes - that is exactly what I am looking for. Thank you!