Search code examples
photoshopphotoshop-script

Enable or disable the selected layer mask


Can anyone help how I can disable or enable the layer mask of the selected layer? I wanted to automate it via scrip not with action.

I can't provide any code yet since I don't know yet what to do.


Solution

  • Scriptlistener is your friend here.

    // Switch off any dialog boxes
    displayDialogs = DialogModes.ERROR; // OFF
    
    layermask(true);
    alert("Layer mask enabled");
    // or
    layermask(false);
    alert("Layer mask disabled");
    
    // Switch back on any dialog boxes
    displayDialogs = DialogModes.ALL; // ON
    
    function layermask(bool)
    {
      // =======================================================
      var idsetd = charIDToTypeID( "setd" );
      var desc22 = new ActionDescriptor();
      var idnull = charIDToTypeID( "null" );
      var ref5 = new ActionReference();
      var idLyr = charIDToTypeID( "Lyr " );
      var idOrdn = charIDToTypeID( "Ordn" );
      var idTrgt = charIDToTypeID( "Trgt" );
      ref5.putEnumerated( idLyr, idOrdn, idTrgt );
      desc22.putReference( idnull, ref5 );
      var idT = charIDToTypeID( "T   " );
      var desc23 = new ActionDescriptor();
      var idUsrM = charIDToTypeID( "UsrM" );
      desc23.putBoolean( idUsrM, bool ); // value set here
      var idLyr = charIDToTypeID( "Lyr " );
      desc22.putObject( idT, idLyr, desc23 );
      executeAction( idsetd, desc22, DialogModes.NO );
    
    }
    

    Using a bit of Action manager code to get the state of the layer mask we can use:

    // Switch off any dialog boxes
    displayDialogs = DialogModes.ERROR; // OFF
    
    
    var layerMaskState = false;
    
    s2t = stringIDToTypeID;
    var r = new ActionReference();
    r.putProperty(s2t('property'), s2t('userMaskEnabled'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    
    // Make sure that the object that executeActionGet
    // returns contains this key (which means the layer has a mask):
    
    var d = executeActionGet(r);
    if (d.hasKey(s2t('userMaskEnabled')))
    {
      layerMaskState = d.getBoolean(s2t('userMaskEnabled'));
    }
    
    // Toggle it!
    layerMaskState = !layerMaskState;
    layer_mask(layerMaskState);
    
    
    // Switch off any dialog boxes
    displayDialogs = DialogModes.ALL; // OFF
    
    
    function layer_mask(bool)
    {
      // =======================================================
      var idsetd = charIDToTypeID( "setd" );
      var desc22 = new ActionDescriptor();
      var idnull = charIDToTypeID( "null" );
      var ref5 = new ActionReference();
      var idLyr = charIDToTypeID( "Lyr " );
      var idOrdn = charIDToTypeID( "Ordn" );
      var idTrgt = charIDToTypeID( "Trgt" );
      ref5.putEnumerated( idLyr, idOrdn, idTrgt );
      desc22.putReference( idnull, ref5 );
      var idT = charIDToTypeID( "T   " );
      var desc23 = new ActionDescriptor();
      var idUsrM = charIDToTypeID( "UsrM" );
      desc23.putBoolean( idUsrM, bool ); // set here
      var idLyr = charIDToTypeID( "Lyr " );
      desc22.putObject( idT, idLyr, desc23 );
      executeAction( idsetd, desc22, DialogModes.NO );
    }