Copy link to clipboard
Copied
I have a script to loop through a document and check on every layer if there is a blank layer mask, and then delete that layer mask.
I'm not sure if there is a best approach to check if a layer mask is blank than getting a selection and inverting it.
After inverting, if there is no selection, Photoshop pops up a warning message stating "No pixels are selected."
How to avoid this message without check "Don't show again"?
for ( var a =0; a<activeDocument.artLayers.length; a++ ){
activeDocument.activeLayer = activeDocument.artLayers;
checkLayerMask();
};
function checkLayerMask() {
// has layer mask
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
var hasLayerMask = desc.hasKey(charIDToTypeID("UsrM")); // bool
if (hasLayerMask) {
// make layer mask active
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Msk " ) );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
// get selection from layer mask
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
desc.putReference( charIDToTypeID( "null" ), ref );
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "T " ), ref );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
// invert selection
activeDocument.selection.invert();
try { activeDocument.selection.bounds}
catch(e) {
// delete active layer mask
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref );
executeAction( charIDToTypeID( "Dlt " ), desc, DialogModes.NO );
};
}
app.activeDocument.selection.deselect();
};
executeAction( charIDToTypeID( "Dlt " ), desc, DialogModes.NO );
};
}
app.activeDocument.selection.deselect();
};
Your code in the first post contains errors at the end.
However, if you fix it and insert it at the very beginning
app.displayDialogs = DialogModes.NO;
then you get what you want.
Copy link to clipboard
Copied
I don't know how to create blank selection, but I'm experiencing it sometimes and I think the best way to check it is blank, is to make path from selection. If that selection is not invisible then path can be created that you may check whether it exists.
Copy link to clipboard
Copied
I think I was not clear in my question, sorry about that.
My understanding about blank mask is a white mask, where the layer is 100% opaque.
The intention deleting white masks is to keep the document tidy without unnecessary masks.
I think I got a solution for my problem getting a selection using 'Select and Mask' tool. So I can check if the mask is 100% black when there is no selection.
This is the revised code:
for ( var a =0; a<activeDocument.layers.length; a++ ){
activeDocument.activeLayer = activeDocument.layers;
alert(activeDocument.layers.name)
checkLayerMask();
};
function checkLayerMask() {
// has layer mask
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
var hasLayerMask = desc.hasKey(charIDToTypeID("UsrM")); // bool
if (hasLayerMask) {
// make layer mask active
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Msk " ) );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
// invert layer mask
var idInvr = charIDToTypeID( "Invr" );
executeAction( idInvr, undefined, DialogModes.NO );
// get selection from layer mask through 'Select and Mask'
var desc = new ActionDescriptor();
executeAction( stringIDToTypeID( "smartBrushWorkspace" ), desc, DialogModes.NO );
try {
activeDocument.selection.bounds;
app.activeDocument.selection.deselect();
// invert layer mask
var idInvr = charIDToTypeID( "Invr" );
executeAction( idInvr, undefined, DialogModes.NO );
}
catch(e) {
// delete active layer mask
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref );
executeAction( charIDToTypeID( "Dlt " ), desc, DialogModes.NO );
};
}
app.activeDocument.selection.deselect();
};
Copy link to clipboard
Copied
Your code in the first post contains errors at the end.
However, if you fix it and insert it at the very beginning
app.displayDialogs = DialogModes.NO;
then you get what you want.
Copy link to clipboard
Copied
thank you,your answer save my life!
Copy link to clipboard
Copied
Thanks, it works
Copy link to clipboard
Copied
I tried your both scripts on new document with 'white' mask created from selection of document bounds and in both cases I had the same result, so a layer mask(s) (if filled whole document, with any opacity & fill level) got deleted with no problem.
I don't know why you had that warning. By the way I aslo removed from end of your first code mistakenly put last 6 lines.
Copy link to clipboard
Copied
Probably you've checked "Don't show again".
Copy link to clipboard
Copied
I guess so, but why didn't you did the same in no time? 🙂
Copy link to clipboard
Copied
Ah, it's just because I think this warning message is useful in my workflow in Photoshop. This is the way how I check when a mask is blank or not.
With this script now, I hope my workflow will be simplified...