Skip to main content
Inspiring
March 1, 2017
Answered

How do I get selected channel indexes?

  • March 1, 2017
  • 4 replies
  • 4189 views

I can get the names of all the currently selected channels in a document with:

app.activeDocument.activeChannels;

But sometimes a document has multiple channels with the identical name. So when I perform actions based on the channels returned this way, I can operate on the wrong channel - a channel that was not selected, but had the same name as one that was selected.

Is there a way to get the indexes of every selected channel, rather than the names?

(Probably not relevant, but I'm actually in Applescript, but this doesn't seem to be in the Applescript dictionary, so I'm actually calling the Javascript at the top

set theChannels to do javascript "app.activeDocument.activeChannels;")

Thanks,

Tom.

This topic has been closed for replies.
Correct answer TomT-shirts

Oops, there actually is an Applescript command for this, I was simply missing it. And it does return the indexes, not the names:

set someVariable to the current channels

That was simple.

4 replies

TomT-shirtsAuthorCorrect answer
Inspiring
March 2, 2017

Oops, there actually is an Applescript command for this, I was simply missing it. And it does return the indexes, not the names:

set someVariable to the current channels

That was simple.

JJMack
Community Expert
Community Expert
March 2, 2017

Yes you got it. Not just the names....

JJMack
Chuck Uebele
Community Expert
Community Expert
March 2, 2017

To select a channel by it's index you need to put it in an array. In my example, I have channel 2, which is the blue channel. They start at 0 for red. Of course, you could add more channels to the array and select multiple channels.

#target photoshop

var doc = activeDocument

var channelArray = [doc.channels[2]]

doc.activeChannels = channelArray

Inspiring
March 2, 2017

https://forums.adobe.com/people/Chuck+Uebele  wrote

To select a channel by it's index you need to put it in an array. In my example, I have channel 2, which is the blue channel. They start at 0 for red. Of course, you could add more channels to the array and select multiple channels.

#target photoshop var doc = activeDocument var channelArray = [doc.channels[2]] doc.activeChannels = channelArray

Thank you for the response, but it appears you are answering the question "How do I select a channel by it's index," which is not something I'm having any trouble with. My question is How do I get the indexes of the currently selected channels?

JJMack
Community Expert
Community Expert
March 1, 2017

I just hack at it, I do not know javascipt like you.  The beginning of what you posted looks like AppleScript  I do not use a mac...

JJMack
Inspiring
March 1, 2017

Yeah, I don't know Javascript either. I know Applescript pretty well.

Maybe someone else will chime in with a solution.

Thanks,

Tom.

JJMack
Community Expert
Community Expert
March 1, 2017

The Javascript  part you posted is not doing anything with channels its dealing with layers???

To get the selected channels I posted the code

I see this in the reference

// remember which channels are currently active

var myActiveChannels = app.activeDocument.activeChannels

I think that would be an array of selected channels

where

var myChannels =  app.activeDocument.channels

would be an array of all channels.

JJMack
JJMack
Community Expert
Community Expert
March 1, 2017

I have seen scripts the find all the selected layers I forget hat the was programmed.  So I would guess that may be a way  to do the with channels.   There may be a channel object attribute  you could test if it is selected the is a visible Boolean I do not know what that means I do not see one named selected

I see this in the reference

// remember which channels are currently active

var myActiveChannels = app.activeDocument.activeChannels

I think that would be an array of selected channels

where

var myChannels =  app.activeDocument.channels

would be all channels

There can only be 53 Alpha channels  adding in the other channels the max not of channels would be under 60.  I do not how if the active layer layer mask that shows in the channels palette  would be  included or not.

JJMack
Inspiring
March 1, 2017

Thanks. I've been unable to find an "is selected" attribute on the channels allowing me to identify them that way.

I have a script that returns the names of all the selected layers, and it is easy to modify to return their index, rather than name (that's what it actually gets first, then gets the names by index). However, changing it to work with channels is either not straight forward, or I just can't do it because I don't really know Javascript, I just use it occasionally for things outside the Applescript dictionary.

Here's the script to get selected layer names, in case it's apparent to you how to adapt it to channels.

on get_selected_layer_names()

  tell application "/Applications/Adobe Photoshop CS6/Adobe Photoshop CS6.app"

  set theSelectedLayers to do javascript "#target Photoshop

main();

function main(){

if(!documents.length) return;

//list of selected layers index's

var selectedLayers = getSelectedLayersIdx();

var layerNames=new Array();

//get layer names and put them in an array along with the index

for(var a in selectedLayers){

    layerNames.push( getNameByIndex(Number(selectedLayers)) );

    }

    //return the information

return layerNames.reverse().join( '\\r' );

}

//Functions

function  getSelectedLayersIdx(){

  var selectedLayers = new Array;

  var ref = new ActionReference();

  ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

  var desc = executeActionGet(ref);

  if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

   desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

    var c = desc.count

    var selectedLayers = new Array();

    for(var i=0;i<c;i++){

    try{

     activeDocument.backgroundLayer;

     selectedLayers.push(  desc.getReference( i ).getIndex() );

    }catch(e){

     selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

    }

    }

   }else{

   var ref = new ActionReference();

   ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));

   ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

   try{

    activeDocument.backgroundLayer;

    selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1);

   }catch(e){

    selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));

   }

  }

  return selectedLayers;

};

function getNameByIndex(idx){

ref = new ActionReference();

ref.putProperty( charIDToTypeID(\"Prpr\") , charIDToTypeID( \"Nm  \" ));

ref.putIndex( charIDToTypeID( \"Lyr \" ), idx );

return executeActionGet(ref).getString(charIDToTypeID( \"Nm  \" ));

};"

  end tell

  return theSelectedLayers

end get_selected_layer_names