Skip to main content
Known Participant
March 1, 2013
Question

How to know if a layer mask is full white ?

  • March 1, 2013
  • 2 replies
  • 755 views

Hi,

I need a script to know if a layer mask is full white, meaning it can be deleted without any visual effect.

I can get layer mask selection, but it allways report full image size

getLayerMaskSelection()

doc.Selection.Bounds

gives (0.0, 0.0, 4000.0, 2400.0) even with doc.Selection.Invert()

Any idea ?

Basically, I try to delete all empty layer masks.

This topic has been closed for replies.

2 replies

Inspiring
March 1, 2013

Here is another way

function selectLayerMask(visible){

    var rc = false;

    try {

        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" ), visible );

        executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

        rc = true;

    } catch (e) {}

    return rc;

};

function isRevealAllMask(){

    var ref = new ActionReference();

    ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

    var desc = executeActionGet(ref).getList(charIDToTypeID( "Hstg" ));

    var histogram =[];

    for(var h=1;h<256;h++){

        histogram.push(desc.getInteger(h));

    }

    return histogram[254] == histogram.toString().match(/[^,0][0-9]*/);

};

function selectComponentChannel() {

    try{

        var map = {}

        map[DocumentMode.GRAYSCALE] = charIDToTypeID('Blck');

        map[DocumentMode.RGB] = charIDToTypeID('RGB ');

        map[DocumentMode.CMYK] = charIDToTypeID('CMYK');

        map[DocumentMode.LAB] = charIDToTypeID('Lab ');

        var desc = new ActionDescriptor();

            var ref = new ActionReference();

            ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), map[app.activeDocument.mode] );

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

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

    }catch(e){}

};

var hasMask = selectLayerMask(true);

if(hasMask){

    var revealAll = isRevealAllMask();

    selectComponentChannel();

    if(revealAll) alert('mask is all white');

}

Also if you have one of the newer versions of Photoshop that support mask density you might also want to check that. If the density is set to 0 this will not show the alert for non-all white mask even though the appear white in the GUI

Nicolas.kAuthor
Known Participant
March 4, 2013

wow, many thanks ! works know, by checking histogram, very cleaver way, thanks.

Paul Riggott
Inspiring
March 1, 2013

Maybe someone has a better way than this?

toggleMask(true);
select();
activeDocument.selection.invert();
if(!hasSelection()) alert("this is a blank layer");
toggleMask();

function hasSelection (doc) {
if(doc == undefined) doc = activeDocument;
  var res = false;
  var as = doc.activeHistoryState;
  doc.selection.deselect();
  if (as != doc.activeHistoryState) {
    res = true;
    doc.activeHistoryState = as;
  }
  return res;
};
function toggleMask(Mask) {
if(Mask == undefined) Mask = false;  
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'), Mask );
executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
};
function select() {
var desc64 = new ActionDescriptor();
var ref39 = new ActionReference();
ref39.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
desc64.putReference( charIDToTypeID('null'), ref39 );
var desc65 = new ActionDescriptor();
desc65.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 2.000000 );
desc65.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 2.000000 );
desc64.putObject( charIDToTypeID('T   '), charIDToTypeID('Pnt '), desc65 );
desc64.putInteger( charIDToTypeID('Tlrn'), 1 );
executeAction( charIDToTypeID('setd'), desc64, DialogModes.NO );
};