-
1. Re: How to check if a layer mask is selected
c.pfaffenbichler Sep 17, 2012 5:29 AM (in response to Harald Heim)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 Sep 17, 2012 5:38 AM (in response to c.pfaffenbichler)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 Sep 17, 2012 8:19 AM (in response to Harald Heim)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 Sep 17, 2012 8:24 AM (in response to Harald Heim)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 Sep 17, 2012 8:42 AM (in response to Michael L Hale)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 Sep 17, 2012 8:43 AM (in response to JJMack)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.
-
7. Re: How to check if a layer mask is selected
Harald Heim Sep 17, 2012 10:15 AM (in response to Michael L Hale)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 Sep 17, 2012 1:24 PM (in response to Harald Heim)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 Sep 17, 2012 2:34 PM (in response to Michael L Hale)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 Sep 17, 2012 4:05 PM (in response to Michael L Hale)Thank You Michael your partipation in thers forums are a great asset. So Thank you very MUCH....


