19 Replies Latest reply on Mar 3, 2017 9:31 PM by JJMack

    How do I get selected channel indexes?

    TomT-shirts Level 1

      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.

        • 1. Re: How do I get selected channel indexes?
          JJMack Most Valuable Participant

          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.

          • 2. Re: How do I get selected channel indexes?
            TomT-shirts Level 1

            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[a])) );

                }

                //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

            • 3. Re: How do I get selected channel indexes?
              JJMack Most Valuable Participant

              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...

              • 4. Re: How do I get selected channel indexes?
                TomT-shirts Level 1

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

                 

                Maybe someone else will chime in with a solution.

                 

                Thanks,

                 

                Tom.

                • 5. Re: How do I get selected channel indexes?
                  JJMack Most Valuable Participant

                  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.

                  • 6. Re: How do I get selected channel indexes?
                    TomT-shirts Level 1

                    Sorry, I'm getting confused about the conversation thread here.

                     

                    My original post said I was using:

                    app.activeDocument.activeChannels;

                     

                    which returns the channels names, but I'd like channel ID's. Isn't the code in my original question the exact same code you're posting when you post

                     

                    JJMack  wrote

                    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

                    And with regard to

                     

                    JJMack  wrote

                     

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

                    I only posted that in response to your comment "I have seen scripts the find all the selected layers I forget hat the was programmed."

                    So I thought maybe seeing how that was programmed would be helpful to you.

                    • 7. Re: How do I get selected channel indexes?
                      JJMack Most Valuable Participant

                      TomT-shirts  wrote

                       

                      Sorry, I'm getting confused about the conversation thread here.

                       

                      My original post said I was using:

                      app.activeDocument.activeChannels;

                       

                      which returns the channels names, but I'd like channel ID's. Isn't the code in my original question the exact same code you're posting when you post

                       

                      I miss read the part of you append for I have never programmed a script to use channels not using the channels names.  I also do not agree with what you write that does not return the channels names. It returns an array of channel objects.  Yes channels names are in channel object and other channel object can have the same name.  What you have I believe is what you want the channels. The channels you want

                      • 8. Re: How do I get selected channel indexes?
                        Chuck Uebele Adobe Community Professional & MVP

                        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
                        
                        • 9. Re: How do I get selected channel indexes?
                          TomT-shirts Level 1

                          TomT-shirts  wrote

                          I miss read the part of you append for I have never programmed a script to use channels not using the channels names.  I also do not agree with what you write that does not return the channels names. It returns an array of channel objects.  Yes channels names are in channel object and other channel object can have the same name.  What you have I believe is what you want the channels. The channels you want

                           

                           

                          What I have returns the channel names.

                           

                          What I'm doing is getting the currently selected channels, then I go through those with a repeat loop and do something to each selected channel.

                           

                          However, multiple channels can have the same name. For instance, there can be two channels both named "Red." Say there was a spot color channel named "Red" that was selected when the script was run. When I go through my repeat loop modifying each channel, my script will not actually modify the channel that was selected (the second, spot-color channel named "Red") it will modify the component channel named "Red," because it has the same name and comes first in the document.

                           

                          If instead of getting the names of the selected channels, I could get the index of the selected channels, I would not have this problem. I would know with certainty what channels I am modifying.

                           

                          Again, my question is: How do I get the indexes of the currently selected channels?

                           

                          • 10. Re: How do I get selected channel indexes?
                            TomT-shirts Level 1

                            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?

                            • 11. Re: How do I get selected channel indexes?
                              JJMack Most Valuable Participant

                              TomT-shirts  wrote

                               

                               

                              What I have returns the channel names.

                               

                              Can you post your code that you state just return just the channels names not instances of channels objects.

                               

                              var myActiveChannels = app.activeDocument.activeChannels
                              ActiveChannelsInfo="";
                              for(var i=0;i<myActiveChannels.length;i++){      
                                ActiveChannelsInfo=ActiveChannelsInfo + "Name=" + myActiveChannels[i].name + " ";
                                ActiveChannelsInfo=ActiveChannelsInfo + "Kind=" + myActiveChannels[i].kind + " ";
                                ActiveChannelsInfo=ActiveChannelsInfo + "Typename=" + myActiveChannels[i].typename + " ";
                                ActiveChannelsInfo=ActiveChannelsInfo + "Visible=" + myActiveChannels[i].visible + "\n";
                              }
                              alert("My Active Channels Info\n" + ActiveChannelsInfo );
                              
                              • 12. Re: How do I get selected channel indexes?
                                TomT-shirts Level 1

                                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.

                                • 13. Re: How do I get selected channel indexes?
                                  JJMack Most Valuable Participant

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

                                  • 14. Re: How do I get selected channel indexes?
                                    TomT-shirts Level 1

                                    The code I've been posting just returns the names:

                                     

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

                                     

                                    The Applescript "current channels" is returning references.

                                    • 15. Re: How do I get selected channel indexes?
                                      JJMack Most Valuable Participant

                                      Run the JavaScript I posted to get an Idea of what "app.activeDocument.activeChannels;" actually is. Its not the names...

                                       

                                       

                                       

                                      var myActiveChannels = app.activeDocument.activeChannels;

                                      ActiveChannelsInfo="";

                                      for(var i=0;i<myActiveChannels.length;i++){     

                                        ActiveChannelsInfo=ActiveChannelsInfo + "Name=" + myActiveChannels[i].name + " ";

                                        ActiveChannelsInfo=ActiveChannelsInfo + "Kind=" + myActiveChannels[i].kind + " ";

                                        ActiveChannelsInfo=ActiveChannelsInfo + "Typename=" + myActiveChannels[i].typename + " ";

                                        ActiveChannelsInfo=ActiveChannelsInfo + "Visible=" + myActiveChannels[i].visible + "\n";

                                      }

                                      alert("My Active Channels Info\n" + ActiveChannelsInfo );

                                      • 16. Re: How do I get selected channel indexes?
                                        TomT-shirts Level 1

                                        Yes, that last script you posted does work to extract more than the names. It is probably relevant then that I'm working in Applescript and posted the Applescript code I'm using to call the Javascript in my first post, because what is returned to Applescript is not references to the channels, it is simply the channel names. Perhaps they are coerced from references to names when returned to Applescript or something? I don't know, I don't work in Javascript, I just call it occasionally when I find things Applescript can't do, which I mistakenly thought was the case here.

                                         

                                        Given the code you provided, I'd have expected it would be possible to get the indexes from the references the way you're getting the name, etc. Something like:

                                         

                                        var myActiveChannels = app.activeDocument.activeChannels;

                                        ActiveChannelsInfo="";

                                        for(var i=0;i<myActiveChannels.length;i++){    

                                          ActiveChannelsInfo=ActiveChannelsInfo + "Index=" + myActiveChannels[i].index + "";

                                        }

                                        alert("My Active Channels Info" + ActiveChannelsInfo );

                                         

                                        But I just get "undefined" out of that. Maybe ".index" is the wrong syntax?

                                         

                                        Not important since I found the native Applescript way, but it seems like the code you posted should be adaptable to pull the indexes from the references so they could be returned, I just couldn't get it. For example, I adapted your code to return the channel type of each channel to Applescript:

                                         

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

                                          do javascript "var myActiveChannels = app.activeDocument.activeChannels;

                                        ActiveChannelsInfo=\"\";

                                        for(var i=0;i<myActiveChannels.length;i++){    

                                          ActiveChannelsInfo=ActiveChannelsInfo + myActiveChannels[i].kind + \", \";

                                        }

                                        ActiveChannelsInfo;"

                                        end tell

                                         

                                        So I suspect there is something very similar that returns the indexes.

                                        • 17. Re: How do I get selected channel indexes?
                                          JJMack Most Valuable Participant

                                          I do not know JavaScript however I can hack at it I have no knowledge of AppleScript.  However I can read some English words in the Apple. I got the Idea your AppleScript was using Javascript   I saw this

                                           

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

                                           

                                          you stated theChannels was set to Names  I keep writing  "NO" that is not what "app.activeDocument.activeChannels;"

                                          is. It is and array containing the channel objects you want  you have theChannels

                                           

                                          If it is one channel  and the kind is ChannelType.SELECTEDAREA it is the channel you want other channel can be the same kind and have the same name this is the active one the one you want.

                                           

                                          This may help  Note the duplicate named one's visibility is false the select one it true

                                          var myActiveChannels = app.activeDocument.activeChannels  
                                          ActiveChannelsInfo="";  
                                          for(var i=0;i<myActiveChannels.length;i++){        
                                            ActiveChannelsInfo=ActiveChannelsInfo + "Name=" + myActiveChannels[i].name + " ";  
                                            ActiveChannelsInfo=ActiveChannelsInfo + "Kind=" + myActiveChannels[i].kind + " ";  
                                            ActiveChannelsInfo=ActiveChannelsInfo + "Typename=" + myActiveChannels[i].typename + " ";  
                                            ActiveChannelsInfo=ActiveChannelsInfo + "Visible=" + myActiveChannels[i].visible + "\n";  
                                          }  
                                          
                                          
                                          var myChannels = app.activeDocument.channels  
                                          ChannelsInfo="";  
                                          for(var i=0;i<myChannels.length;i++){        
                                            ChannelsInfo=ChannelsInfo + "Name=" + myChannels[i].name + " ";  
                                            ChannelsInfo=ChannelsInfo + "Kind=" + myChannels[i].kind + " ";  
                                            ChannelsInfo=ChannelsInfo + "Typename=" + myChannels[i].typename + " ";  
                                            ChannelsInfo=ChannelsInfo + "Visible=" + myChannels[i].visible + "\n";  
                                          }  
                                          alert("My Active Channels Info\n" + ActiveChannelsInfo + "\nMy Channels Info\n" + ChannelsInfo );  
                                          

                                           

                                          Capture.jpg

                                          Capture.jpg

                                          Capture.jpg

                                          • 18. Re: How do I get selected channel indexes?
                                            TomT-shirts Level 1

                                            JJMack  wrote

                                             

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

                                             

                                            you stated theChannels was set to Names  I keep writing  "NO" that is not what "app.activeDocument.activeChannels;"

                                            is. It is and array containing the channel objects you want  you have theChannels

                                             

                                            I'm not sure how to explain this, I keep writing it. What is returned to Applescript is the names of the channels, not references to them.

                                             

                                            Untitled 4.png

                                             

                                            Since they are references before they get returned to Applescript, a javascript could probably get the indexes from the references and then return them, but I don't know how to do that.

                                            • 19. Re: How do I get selected channel indexes?
                                              JJMack Most Valuable Participant

                                              Not many here use Apple script I use Windows AppleScript I know nothing about The javascript you used in your AppleScript would set a JavaScript Variable Array of Photoshop Channel objects