10 Replies Latest reply on Sep 17, 2012 4:05 PM by JJMack

    How to check if a layer mask is selected

    Harald Heim Level 1

      I know how to check if a layer has a layer mask and how to select or unselect it, but how do I check from a script if a layer mask is selected in the Layer panel?

       

      I did not find any information about that anywhere. I hope it is possible.

        • 1. Re: How to check if a layer mask is selected
          c.pfaffenbichler Level 9

          app.activeDocument.activeChannels provides an Array of the active Channels.

           

          Edit: It might be necessary to determine the number of channels that make up an image (3 for RGB, 4 for CMYK etc.).

          • 2. Re: How to check if a layer mask is selected
            Harald Heim Level 1

            Thanks. I now came up with this code:

             

                try{

                    if (app.activeDocument.activeChannels) alert("Layer mask is not selected");

                } catch(e) {

                    alert("Layer mask is selected");

               }

             

            When a layer mask is selected and you try to access app.activeDocument.activeChannels an exception is raised,  so I had to add the try/catch stuff. Is there a more elegant solution without try/catch?

            • 3. Re: How to check if a layer mask is selected
              Michael L Hale Level 5

              If you are willing to use Action Manager you can avoid the try/catch.

               

              var ref = new ActionReference();
              ref.putEnumerated( charIDToTypeID("Chnl"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
              var channelName = executeActionGet(ref).getString(charIDToTypeID("ChnN"));
              

               

              If the layer is selected channelName will be the name of the composite channel( RGB, CMYK, etc. ). If the mask is selected it will be the name of the channel mask which is normally layerName + ' ' + Mask( at least in English versions of Photoshop ).

              • 4. Re: How to check if a layer mask is selected
                JJMack Most Valuable Participant

                If you consider a selected vecter layer mask as the current target to be a selected a layer mask then that code does not work to there are active channels in that case and an exception will not happen.

                • 5. Re: How to check if a layer mask is selected
                  JJMack Most Valuable Participant

                  If the selected target is a layers vector mask that code returns RGB(channel) and not "Layer Name Vector Mask".  So I think some extra code is needed to check if the layer's current target is the vector mask.

                   

                  It Also looks like in CS5 and CS6 I can target both Layer Mask on a layer at the same time.

                  • 6. Re: How to check if a layer mask is selected
                    Michael L Hale Level 5

                    You are right JJMack, I didn't consider a vector mask.

                     

                    The following will works correctly to test if a channel mask is selected. It works if there no channel mask, only an channel mask, or both channel and vector masks.

                     

                    function layerMaskName(){
                        ref = new ActionReference();
                        args = new ActionDescriptor();
                        ref.putProperty( charIDToTypeID('Prpr'), charIDToTypeID('ChnN') );
                        ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Ordn'), charIDToTypeID('Msk ') );
                        args.putReference( charIDToTypeID('null'), ref );
                    
                        var resultDesc = executeAction( charIDToTypeID('getd'), args, DialogModes.NO );
                        return resultDesc.getString( charIDToTypeID('ChnN') );
                    };
                    function isChannelMaskSelected(){
                        var ref = new ActionReference();
                        ref.putEnumerated( charIDToTypeID("Chnl"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                        var desc = executeActionGet(ref).getString(charIDToTypeID("ChnN"));
                        return desc === layerMaskName() ? true:false;
                    };
                    isChannelMaskSelected();
                    

                     

                    That also gets around the color mode and localizaton issues of my first suggestion.

                     

                    Another test is needed to check if a vector mask is selected.

                    1 person found this helpful
                    • 7. Re: How to check if a layer mask is selected
                      Harald Heim Level 1

                      Thanks a lot, Michael! Your code seems to works perfectly, even if a vector mask is present. I do not care if a vector mask is selected or not as the vector mask selection seems to be independent of the image/mask selection.

                      • 8. Re: How to check if a layer mask is selected
                        Michael L Hale Level 5

                        For JJMack

                         

                        function isVectorMaskSelected(){
                            return activePathIndex() != -1 ? true:false;
                        };
                        function activePathIndex(){
                            var ref = new ActionReference();
                            ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                            var desc = executeActionGet( ref );
                            return  desc.hasKey(charIDToTypeID("TrgP" )) ? desc.getInteger(charIDToTypeID("TrgP" )):-1;
                        };
                        isVectorMaskSelected();
                        
                        • 9. Re: How to check if a layer mask is selected
                          Michael L Hale Level 5

                          Oops, if a path is selected that is not the vector mask my last code will still return true( with the vector mask not selected ). I think this corrects that.

                           

                          function layerVectorMaskName(){
                              ref = new ActionReference();
                              args = new ActionDescriptor();
                              ref.putProperty( charIDToTypeID('Prpr'), charIDToTypeID('PthN') );
                              ref.putEnumerated( charIDToTypeID('Path'), charIDToTypeID('Path'), stringIDToTypeID('vectorMask') );
                              args.putReference( charIDToTypeID('null'), ref );
                          
                              var resultDesc = executeAction( charIDToTypeID('getd'), args, DialogModes.NO );
                              return resultDesc.getString( charIDToTypeID('PthN') );
                          };
                          function isVectorMaskSelected(){
                              return activePathName() == layerVectorMaskName()  ? true:false;
                          };
                          function activePathName(){
                              var ref = new ActionReference();
                              ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                              var desc = executeActionGet( ref );
                              if( desc.hasKey(charIDToTypeID("TrgP" )) && desc.getInteger(charIDToTypeID("TrgP" )) != -1 ){
                                  return app.activeDocument.pathItems[desc.getInteger(charIDToTypeID("TrgP" ))].name;
                              }
                          };
                          isVectorMaskSelected()
                          
                          • 10. Re: How to check if a layer mask is selected
                            JJMack Most Valuable Participant

                            Thank You Michael your partipation in thers forums are a great asset.  So Thank you very MUCH....