3 Replies Latest reply on Mar 4, 2013 12:13 AM by Nicolas.k

    How to know if a layer mask is full white ?

    Nicolas.k

      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.

        • 1. Re: How to know if a layer mask is full white ?
          Paul Riggott Level 6

          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 );
          };
          
          
          • 2. Re: How to know if a layer mask is full white ?
            Michael L Hale Level 5

            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

            • 3. Re: How to know if a layer mask is full white ?
              Nicolas.k Level 1

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