8 Replies Latest reply on Oct 24, 2011 11:27 PM by BlackpainterAdi

    Get Selection from Layer

    dgolberg Level 1

      How can one get a selection based on a specific layer's transparency using script?  Basically, I'm looking for the same result as when one ctrl+clicks a layer's thumbnail in the Layers window.  Thanks for any help!

       

       

      dgolberg

        • 1. Re: Get Selection from Layer
          Paul Riggott Level 6

          Please try this...

           

           

          selectArea();
          function selectArea() {
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
          desc.putReference( charIDToTypeID('null'), ref );
          var ref2 = new ActionReference();
          ref2.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
          desc.putReference( charIDToTypeID('T   '), ref2 );
          executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
          };
          
          
          • 2. Re: Get Selection from Layer
            dgolberg Level 1

            Hmm, it always tries to open the "Load Selection" window rather than create a selection from a layer.  Here's the code I use to get the layer I want to create the selection from:

             

            var targetSelection = app.activeDocument.layerSets.getByName("stone").layers.getByName("mask");

            • 3. Re: Get Selection from Layer
              Paul Riggott Level 6

              Is your code actually selecting the correct layer? Is the layer named "mas k"

              Does the layer have a mask?

               

              Try the code i gave by itself on a selected layer and see if that works for you.

              • 4. Re: Get Selection from Layer
                dgolberg Level 1

                The layer is named "mask" (for some strange reason, the forums add that space, yet when editing the post, there is no space). 

                 

                Trying the code on it's own still brings up the "Load Selection" dialog, but it now correctly brings up the option to load selection from the transparency.  Is there any way to get it to create the selection without it bringing up the "Load Selection" window?

                • 5. Re: Get Selection from Layer
                  Paul Riggott Level 6

                  That is strange it just selects the area with no window for me, I have tested it with CS3/4 and 5.

                   

                  I wonder what Photoshop version you are using?

                  • 6. Re: Get Selection from Layer
                    dgolberg Level 1

                    Hmm, strange.  I'm using the latest version of CS4 btw.

                     

                    Here's the exact code I'm using:

                     

                    #target photoshop

                    var targetSelection = app.activeDocument.layerSets.getByName("stone").layers.getByName("mask");

                     

                    app.activeDocument.activeLayer = targetSelection;

                     

                    selectArea();

                    function selectArea() {

                    var desc = new ActionDescriptor();

                    var ref = new ActionReference();

                    ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );

                    desc.putReference( charIDToTypeID('null'), ref );

                    var ref2 = new ActionReference();

                    ref2.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );

                    desc.putReference( charIDToTypeID('T   '), ref2 );

                    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );

                    };

                     

                     

                    well, exact except for the weird error where it puts a space in "mask" -.-;

                    • 7. Re: Get Selection from Layer
                      Michael L Hale Level 5

                      As Paul says it should work. The code posted is the output of the scriptlistener plug-in when ctrl clicking on the layer icon. Even if the wrong layer is active you should not get the Load Selection dialog. Say if the background was the activeLayer you would get an error. The only way I can get the Load Selection dialog is to change executeAction( charIDToTypeID('setd'), desc, DialogModes.NO ); to executeAction( charIDToTypeID('setd'), desc, DialogModes.ALL );

                       

                      What happens in the GUI when you ctrl( or whatever the Mac meta key is ) click on that layer's icon?

                      • 8. Re: Get Selection from Layer
                        BlackpainterAdi

                        You could try to copy and paste the Layer into a new AlphaChannel and then load the selection of the respective Alpha Channel.

                         

                        var Doc2 = activeDocument

                         

                        Doc2.selection.selectAll()

                        Doc2.selection.copy(true)

                        Doc2.selection.deselect()

                        Chan = Doc2.channels.add()

                        Doc2.paste()

                        refSel = Doc2.selection.load(Doc2.channels.getByName("Alpha 1"))