skip to Main Content

I need to create a Photoshop script (I’m using Java Script) that takes a few images and applies the same mask to all of them.

(Here is what I mean by applying a mask)

Once I’ve loaded the images using this code

var sourceImageDoc = File("./image.png"))
var maskImageDoc = File("./mask.png"))

how I can set maskImageDoc to be the mask for sourceImageDoc?



  1. Here is some snippets out of one of my scripts that works CS3 +. As you can see it uses the ugly script listener code – if you’re applying it to a layer rather than a group, you may need to adjust that part? I’m not sure I remember ever using that function on a layer rather than a layerset.

    //mask group to paper area 
    app.activeDocument.activeLayer = lyr;   
    app.activeDocument.activeLayer = grp;
    //Selects the contents of the active layer.
    function selectRasterLayerContents() {
    var id47 = charIDToTypeID( "setd" );
        var desc11 = new ActionDescriptor();
        var id48 = charIDToTypeID( "null" );
            var ref11 = new ActionReference();
            var id49 = charIDToTypeID( "Chnl" );
            var id50 = charIDToTypeID( "fsel" );
            ref11.putProperty( id49, id50 );
        desc11.putReference( id48, ref11 );
        var id51 = charIDToTypeID( "T   " );
            var ref12 = new ActionReference();
            var id52 = charIDToTypeID( "Chnl" );
            var id53 = charIDToTypeID( "Chnl" );
            var id54 = charIDToTypeID( "Trsp" );
            ref12.putEnumerated( id52, id53, id54 );
        desc11.putReference( id51, ref12 );
    executeAction( id47, desc11, DialogModes.NO );
    //adds a mask revealing the selection to the active group
    function AddMaskToGroup() {
        var id42 = charIDToTypeID( "Mk  " );
        var desc8 = new ActionDescriptor();
        var id43 = charIDToTypeID( "Nw  " );
        var id44 = charIDToTypeID( "Chnl" );
        desc8.putClass( id43, id44 );
        var id45 = charIDToTypeID( "At  " );
            var ref10 = new ActionReference();
            var id46 = charIDToTypeID( "Chnl" );
            var id47 = charIDToTypeID( "Chnl" );
            var id48 = charIDToTypeID( "Msk " );
            ref10.putEnumerated( id46, id47, id48 );
        desc8.putReference( id45, ref10 );
        var id49 = charIDToTypeID( "Usng" );
        var id50 = charIDToTypeID( "UsrM" );
        var id51 = charIDToTypeID( "RvlS" );
        desc8.putEnumerated( id49, id50, id51 );
    executeAction( id42, desc8, DialogModes.NO );
    Login or Signup to reply.
  2. Applying a layer mask is a pain, because script listener won’t hear it directly.

    You’ll want this then

    function applyLayerMask()
    var id1949 = charIDToTypeID( "Dlt " );
    var desc398 = new ActionDescriptor();
    var id1950 = charIDToTypeID( "null" );
    var ref291 = new ActionReference();
    var id1951 = charIDToTypeID( "Chnl" );
    var id1952 = charIDToTypeID( "Chnl" );
    var id1953 = charIDToTypeID( "Msk " );
    ref291.putEnumerated( id1951, id1952, id1953 );
    desc398.putReference( id1950, ref291 );
    var id1954 = charIDToTypeID( "Aply" );
    desc398.putBoolean( id1954, true );
    executeAction( id1949, desc398, DialogModes.NO );
    Login or Signup to reply.
  3. Here’s my code to apply a layer mask to the currently selected object (provided it has a selection).

    const layerMask = function () {
        const makeID = stringIDToTypeID('make')
        const newID = stringIDToTypeID('new')
        const channelID = stringIDToTypeID('channel')
        const atID = stringIDToTypeID('at')
        const usingID = stringIDToTypeID('using')
        const userMaskEnabledID = stringIDToTypeID('userMaskEnabled')
        const revealSelectionID = stringIDToTypeID('revealSelection')
        const actionDesc = new ActionDescriptor()
        const actionRef = new ActionReference()
        actionDesc.putClass(newID, channelID)
        actionRef.putEnumerated(channelID, channelID, maskID)
        actionDesc.putReference(atID, actionRef)
        actionDesc.putEnumerated(usingID, userMaskEnabledID, revealSelectionID)
        executeAction(makeID, actionDesc, DialogModes.NO)

    Tested in Photoshop CC 2021

    Login or Signup to reply.
  4. I’m late to the party, but here’s my simple method for adding a mask to a layer in ExtendScript. The trick here is to place a copy of the mask just below the image, then merge the image into it. Note that the merge will destroy the original layer object and create a new one, so I always assign the result back to my original variable; otherwise it will become undefined.

    // imageLayer: an artLayer in imageDoc that I want to mask
    // maskLayer:  an artLayer in maskDoc w/ just the mask to be added, no image data
    // if both are in same file, you may not need the activeDocument assignments
    app.activeDocument = maskDoc;
    app.activeDocument = imageDoc;
    imageLayer = imageLayer.merge();

    Now, this just adds the mask to the image layer non-destructively (i.e., it can be removed layer if you like). If you want to truly apply the mask (permanently conform the image to the mask while deleting the mask), then just duplicate a completely blank layer (no mask) below the masked merge-result from the first step above, and then merge that masked result one more time down into the blank layer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top