Skip to main content
July 1, 2016
Beantwortet

Identify and apply mask by RVlS or RVlA

  • July 1, 2016
  • 2 Antworten
  • 465 Ansichten

Hello everybody, I'm having trouble changing this code. He applies a mask by a selection ('RVlS'), but if you haven't a selection, it displays an error.

I need that if there is no selection, then it creates an empty mask ('RVlA'). Can not understand how to put together this "if / else"

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

function Temp() {

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putClass(cTID('Nw  '), cTID('Chnl'));

    var ref1 = new ActionReference();

    ref1.putEnumerated(cTID('Chnl'), cTID('Chnl'), cTID('Msk '));

    desc1.putReference(cTID('At  '), ref1);

    desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID('RvlS'));

    executeAction(cTID('Mk  '), desc1, dialogMode.NO);

  };

 

  step1();

};

Temp.main = function () {

  Temp();

};

Temp.main();

Dieses Thema wurde für Antworten geschlossen.
Beste Antwort von

Finally I got it! I am a bad programmer, but the adobe help is as bad as

cTID = function(s) { return app.charIDToTypeID(s); }; 
sTID = function(s) { return app.stringIDToTypeID(s); }; 

   

function Temp() { 

var SelY = ('RvlS');
var SelN =  ('RvlA');

  function step1(withDialog, doc) { 
   
    if(doc == undefined) doc = activeDocument;
    var as = doc.activeHistoryState;
    doc.selection.deselect();

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); 
    var desc1 = new ActionDescriptor(); 
    desc1.putClass(cTID('Nw  '), cTID('Chnl')); 
    var ref1 = new ActionReference(); 
    ref1.putEnumerated(cTID('Chnl'), cTID('Chnl'), cTID('Msk ')); 
    desc1.putReference(cTID('At  '), ref1); 

    if (as != doc.activeHistoryState) {
    desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(SelY));
    doc.activeHistoryState = as;
    }
    else {
        desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(SelN)); ;
    }

    executeAction(cTID('Mk  '), desc1, dialogMode.NO); 
  }; 
  
  step1();  
}; 
Temp.main = function () { 
  Temp(); 
}; 
Temp.main(); 

2 Antworten

Antwort
July 4, 2016

Finally I got it! I am a bad programmer, but the adobe help is as bad as

cTID = function(s) { return app.charIDToTypeID(s); }; 
sTID = function(s) { return app.stringIDToTypeID(s); }; 

   

function Temp() { 

var SelY = ('RvlS');
var SelN =  ('RvlA');

  function step1(withDialog, doc) { 
   
    if(doc == undefined) doc = activeDocument;
    var as = doc.activeHistoryState;
    doc.selection.deselect();

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); 
    var desc1 = new ActionDescriptor(); 
    desc1.putClass(cTID('Nw  '), cTID('Chnl')); 
    var ref1 = new ActionReference(); 
    ref1.putEnumerated(cTID('Chnl'), cTID('Chnl'), cTID('Msk ')); 
    desc1.putReference(cTID('At  '), ref1); 

    if (as != doc.activeHistoryState) {
    desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(SelY));
    doc.activeHistoryState = as;
    }
    else {
        desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(SelN)); ;
    }

    executeAction(cTID('Mk  '), desc1, dialogMode.NO); 
  }; 
  
  step1();  
}; 
Temp.main = function () { 
  Temp(); 
}; 
Temp.main(); 
JJMack
Community Expert
Community Expert
July 5, 2016

This may work better....

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// ensure at least one document open

if (!documents.length) {  alert('There are no documents open.', 'No Document');}

// if at least one document exists, then proceed

else {  main();}

///////////////////////////////////////////////////////////////////////////////

// main - main function

///////////////////////////////////////////////////////////////////////////////

function main() {

   try {

      cTID = function(s) { return app.charIDToTypeID(s); };

      sTID = function(s) { return app.stringIDToTypeID(s); };

      var SelY = ('RvlS');

      var SelN =  ('RvlA');

      var desc1 = new ActionDescriptor();

      desc1.putClass(cTID('Nw  '), cTID('Chnl'));

      var ref1 = new ActionReference();

      ref1.putEnumerated(cTID('Chnl'), cTID('Chnl'), cTID('Msk '));

      desc1.putReference(cTID('At  '), ref1);

      if (hasSelection()) { desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(SelY));}

      else { desc1.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(SelN)); }

      try { executeAction(cTID('Mk  '), desc1, DialogModes.NO);}

      catch(e) { alert("No Layer or more than one layer was targeted"); }

      }

   // display error message if something goes wrong

   catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

}

///////////////////////////////////////////////////////////////////////////////

// Helper functions

///////////////////////////////////////////////////////////////////////////////

////// check for selection //////

function hasSelection(){

   var ref = new ActionReference();

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

   var docDesc = executeActionGet(ref);

   var hasSelection = docDesc.hasKey(stringIDToTypeID("selection"));

   return hasSelection;

};

JJMack
July 5, 2016

Thanks, JJMack this "fragment" is part of one of the scripts of a package of tools that I developed for use in 3D. I'm making some fine adjustments to the tools, I believe that soon I can come here again.
nMaker Tools

JJMack
Community Expert
Community Expert
July 1, 2016

That code looks like an action that was converted to a script using Xtools.   Action steps are hard coded.  That one add the current selection as a layer ,ask to the target layer.

You could have gotten similar code be installing the Adobe Scriptlistener Plug-in .  Make a selection and the click the add layer ,ask icon in the layers palette.  This add layer mask icon button can ad 4 different  mask types.

It there is no selection it will add a reveal all layer mask (all white) however, if you through in the Alt modifier key into  the mix it will add a hide all layer mask (all black).

If there is as selection when you click the button the selection will be added as a layer mask. The area the is selected will be white and areas not selected will be black. If the is any feathering in the selection that area will be shades of gray. However if you through the Alt modifier key the Layer mask added will be the inverse of the selection.

With the scriptlistener installed you would see the following script code recorded.

// =============reveal all==========================================

var idMk = charIDToTypeID( "Mk  " );

    var desc155 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc155.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref31 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref31.putEnumerated( idChnl, idChnl, idMsk );

    desc155.putReference( idAt, ref31 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idRvlA = charIDToTypeID( "RvlA" );

    desc155.putEnumerated( idUsng, idUsrM, idRvlA );

executeAction( idMk, desc155, DialogModes.NO );

// ===========hide all============================================

var idMk = charIDToTypeID( "Mk  " );

    var desc159 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc159.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref33 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref33.putEnumerated( idChnl, idChnl, idMsk );

    desc159.putReference( idAt, ref33 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idHdAl = charIDToTypeID( "HdAl" );

    desc159.putEnumerated( idUsng, idUsrM, idHdAl );

executeAction( idMk, desc159, DialogModes.NO );

// =============Current selection==========================================

var idMk = charIDToTypeID( "Mk  " );

    var desc178 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc178.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref37 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref37.putEnumerated( idChnl, idChnl, idMsk );

    desc178.putReference( idAt, ref37 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idRvlS = charIDToTypeID( "RvlS" );

    desc178.putEnumerated( idUsng, idUsrM, idRvlS );

executeAction( idMk, desc178, DialogModes.NO );

// ===========hide selection============================================

var idMk = charIDToTypeID( "Mk  " );

    var desc197 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc197.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref41 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref41.putEnumerated( idChnl, idChnl, idMsk );

    desc197.putReference( idAt, ref41 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idHdSl = charIDToTypeID( "HdSl" );

    desc197.putEnumerated( idUsng, idUsrM, idHdSl );

executeAction( idMk, desc197, DialogModes.NO );

You will see the code for all four layer mask types is the same except for one hard coded var. So you has options you can use a try catch to catch the error and when the error is caught you could add a reveal all or hide all mask depend on what you want to do or do nothing and leave the layer unmask.

You could also write a AddLyerMask(MaskType) function where you pass the layer type"RvlA", "HdAl", "RvlS" or "HdSl" but you better has a selection when the type calls for one.

function AddLayerMask(MaskType) {

var idMk = charIDToTypeID( "Mk  " );

    var desc155 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc155.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref31 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref31.putEnumerated( idChnl, idChnl, idMsk );

    desc155.putReference( idAt, ref31 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idMT = charIDToTypeID( MaskType );

    desc155.putEnumerated( idUsng, idUsrM, idMT );

executeAction( idMk, desc155, DialogModes.NO );

}

JJMack