Skip to main content
Participant
March 4, 2018
Question

Javascript for photoshop

  • March 4, 2018
  • 1 reply
  • 1850 views

I have been programming a .jsx to automate some tasks in photoshop CC.  I have been successful in all of the tasks that I needed to do except for one.

I need to create a layermask, which is easy to do via the UI, but I can't quite figure out how to actually make the mask show up and link it to the layer the way I do it in the UI.   I can create the selections and invert in my program, just like I do in the UI.  However, in the UI I then select the tiny "add layer mask " icon on the bottom of the layers section and a layer mask is automatically added to the layer that I had previously selected.  I also found that I can actually do a paste with the inverted selection when in the UI, but it adds the mask to a layer and I don't know how to access that layer programmatically.  Is it really a channel?

I want to translate that action into code.  If I create a channel as a masked area do I link it to the layer that I want? 

Any help will be appreciated, I have been reading references and  searching endlessly.

Message was edited by: Jeanne Kendall I used the addMask function that was posted here and it worked. However, now I just need to use the maskedarea that I had created. Thanks!

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
March 5, 2018

If You install Adobe Plug-in Scriptlistener anything you do in Photoshop the can be recorded in an action will be recorded into two log files on your desktop one is recorded in VBS code the other in JavaScript Code.  It is Action Manager code all variables used are hard coded in the recorded code.    Many Photoshop Features are not covered in Adobe  Photoshop JavaScript DOM.  So often Action manager Code need to be used when scripting Photoshop.  Code recorded by the Scriptlistener Plug-in copied into your Photoshop and often changer into function so variables can be passed to the coded functions.

ScriptListener code is not easy to read or understand.  If you have seen cord in some Photoshop JavaScript you did not understand its was most likely Action manager code.

To add the current active selection as a linked layer mask could look something like this;

// =======linked layer Mask========================================

function linkedlayerMask() {

var idMk = charIDToTypeID( "Mk  " );

    var desc3 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc3.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref3 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref3.putEnumerated( idChnl, idChnl, idMsk );

    desc3.putReference( idAt, ref3 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idRvlS = charIDToTypeID( "RvlS" );

    desc3.putEnumerated( idUsng, idUsrM, idRvlS );

executeAction( idMk, desc3, DialogModes.NO );

}

// =======Un linked layer Mask========================================

function layerMask() {

var idMk = charIDToTypeID( "Mk  " );

    var desc3 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

    var idChnl = charIDToTypeID( "Chnl" );

    desc3.putClass( idNw, idChnl );

    var idAt = charIDToTypeID( "At  " );

        var ref3 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idMsk = charIDToTypeID( "Msk " );

        ref3.putEnumerated( idChnl, idChnl, idMsk );

    desc3.putReference( idAt, ref3 );

    var idUsng = charIDToTypeID( "Usng" );

    var idUsrM = charIDToTypeID( "UsrM" );

    var idRvlS = charIDToTypeID( "RvlS" );

    desc3.putEnumerated( idUsng, idUsrM, idRvlS );

executeAction( idMk, desc3, DialogModes.NO );

// =======================================================

var idsetd = charIDToTypeID( "setd" );

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref1.putEnumerated( idLyr, idOrdn, idTrgt );

    desc2.putReference( idnull, ref1 );

    var idT = charIDToTypeID( "T   " );

        var desc3 = new ActionDescriptor();

        var idUsrs = charIDToTypeID( "Usrs" );

        desc3.putBoolean( idUsrs, false );

    var idLyr = charIDToTypeID( "Lyr " );

    desc2.putObject( idT, idLyr, desc3 );

executeAction( idsetd, desc2, DialogModes.NO );

}

JJMack
Participant
March 5, 2018

Thanks!  I was thinking  of installing it, but hadn't tried yet.   We are using a set of pre-designed .psd files that I just created a script to automate copying pictures into the files.  Some needed to use a mask so that the human could adjust pictures if necessary.  I just realized this am  that I could just add the layer mask to the templates that need them once and I don't have to do it in code.  However, I will use the listener because I want to see exactly what I need to do.

And yes -- I kept hoping that I would not have to learn the Action Manager code -- but I am geting more familiar with it now.!

Kukurykus
Legend
March 5, 2018

I didn't ask you if the scripts are available for free -- and I realize they may not be --

Sorry


That's first man from months I'm here that said something that could be good bonus for writing scripts with more power

Your temporary embarrassment will be surely only gift that we can expect from people looking for fast & gratutious help