Skip to main content
Inspiring
January 23, 2021
Answered

Layer Mask Preview Toggle Script PS2021

  • January 23, 2021
  • 1 reply
  • 1877 views

On this forum, I found a function by Michael Hale that selects and previews a layer mask. I edited the function to create another function that turns the mask preview off.  When the function runs the mask preview is on/off depending on the alert call.

 

Next, I try to create a mask preview toggle through an if statement to turn the mask preview on/off.

if the mask preview is on then run the maskPreviewOff() function.

else if the mask preview is off then run the maskPrevicwOn() function

 

So far I have not been able to make the mask toggle work and I am not sure if it is possible.

Can someone please look at the code and advise if the mask preview toggle can be done vis script? 

 

 

//alert(maskPreviewOff()); // returns true
//alert(maskPreviewOn()); // returns false

  if(maskPreviewOff() == true) {
    alert("The mask is off");
  } else if (maskPreviewOn() == true){
    alert("The mask is on");
  }
else{}


///////////////////////////
// FUNTIONS
///////////////////////////


function maskPreviewOn(){
    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" ), true );
      executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
      rc = true;
    } catch (e) {
      if (!e.toString().match(/Select.+is not currently available/)) {
        throw e;
      }
    }
    return rc;
  }


  function maskPreviewOff(){
    var rc = true;
    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" ), false );
      executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
      rc = false;
    } catch (e) {
      if (!e.toString().match(/Select.+is not currently available/)) {
        throw e;
      }
    }
    return  rc;
  }

 

 

 

 

 

 

 

This topic has been closed for replies.
Correct answer Kukurykus

The new code does produce a true/false boolean. True if the mask is enabled and false if the mask is disabled with a red cross. However, I am trying to toggle between the mask enabled with preview off and mask enabled with a preview on (back and white mask presentation). 

 

When all channels are visible and the mask preview is off the alert returns true.

When all channels are visible and the mask preview is on the alert returns true.

 

Can the code return a true if the mask preview in black and white is on and false if the mask preview in black and white is off?  


Now when mask is enabled it alerts true with preview on, and false with preview off:

 

sTT = stringIDToTypeID; (ref  = new ActionReference()).putProperty(sTT('property'),
json = sTT('json')); ref.putEnumerated(sTT('document'), sTT('ordinal'), sTT('targetEnum'))
evl = eval('(' + executeActionGet(ref).getString(json) + ')'), id = activeDocument.activeLayer.id

function vsbl() {
	(ref  = new ActionReference()).putEnumerated
	(sTT('channel'), sTT('channel'), sTT('mask'))
	return executeActionGet(ref).getBoolean(sTT('visible'))
}

(function(v) {
	for(var i = v.length - 1; i > -1;) {
		if ((lyr = v[i--]).id == id && (msk = lyr.mask) &&
		!(typeof msk.enabled == 'boolean')) return !alert(vsbl())
		if (lyr.type == 'layerSection') {if (callee(lyr.layers)) return true}
	}
})(evl.layers)

 

 

1 reply

Kukurykus
Legend
January 23, 2021
sTT = stringIDToTypeID; (ref  = new ActionReference()).putProperty
(sTT('property'), json = sTT('json')); ref.putClass(sTT('layer'))
evl = eval('(' + executeActionGet(ref).getString(json) + ')')
alert(!(typeof evl.layers[0].mask.enabled == 'boolean'))
Inspiring
January 23, 2021

Thank you for sending the snippet. I am not sure how to interpret this code. For example, I try the snippet on a PSD file with a layer mask, without a layer mask, with the layer mask disabled/enabled, layer mask preview on/off, and in all cases, the alert returns true even when there's no mask on the file. Should the alert return false if there is no mask on the file?

How can I apply the snippet to the script to toggle the mask preview on/off? 

Inspiring
January 24, 2021

Well, if it doesn't matter, then

 

var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("mask"));

var viz = executeActionGet(r).getBoolean(stringIDToTypeID("visible"));

alert(viz)

OK, this is great, I can work with this code. The alert returns true if the mask preview is on and false if the mask preview is off.

Ladies and gentlemen thank you very much for your awesome help, dedication, and generosity!