Skip to main content
Participant
April 3, 2014
Answered

Script to add layer mask to all layers but background?

  • April 3, 2014
  • 1 reply
  • 12293 views

I've been a professional editor/retoucher for photographers for the past three years. I use a lot of actions, but I've never branched into scripts until now. I'm trying to refine my workflow and scripts seem like the next logical step.

I do a lot of interiors work, which is basically manual HDR. Here's my current build process, which I'd love to find a way to script. I tried to be very clear, but let me know if this needs fleshing out. I'm using Photoshop CS5.

1. Pull up bracketed, color-corrected TIFFs in Bridge.

2. Re-order as needed, with main exposure at the bottom. Click "Tools" > "Photoshop" > "Load as Photoshop layers" to get a PSD with layers in correct order and main exposure as background (base) layer.

3. Manually go through each layer and add a Hide All layer mask to every layer except the background layer.

4. Save as a PSD, using the original name of the background layer as the filename. To do this I just copy the layer name (example.tiff), paste in the Save dialog and then use the dropdown menu to select PSD (thus saving as example.psd).

I'm hoping to automate steps 3 and 4 as they are the most time consuming for me. Any input is appreciated!!

Thanks again.

This topic has been closed for replies.
Correct answer Chuck Uebele

Yes your change is good.  That way the last layer won't have a mask applied.  I wasn't sure if you wanted that, but put in the check for the background layer. 

As far as saving, what file format do you want to save to?  Here's how I save a psd:

var docRef = activeDocument

var doneFolder = new Folder('/c/photos/')

var layerName = docRef.activeLayer.name

var psdOptions = new PhotoshopSaveOptions();

psdOptions.layers = true;

app.displayDialogs = DialogModes.ALL;//include this line if you want the dialog box to come up.

docRef.saveAs (new File(doneFolder +'/' + layerName + '.psd'), psdOptions);

app.displayDialogs = DialogModes.NO;//Resets the ALL - you don't want to leave that on.

1 reply

Chuck Uebele
Community Expert
Community Expert
April 3, 2014

You can try this.  Just set a snapshop first incase it does something funky that I didn't catch.  I should note that this will only apply the mask to layers that are not in a group.

 

#target photoshop

 

if(app.documents.length>0){

    var docRef = activeDocument;

    var layerNum = docRef.layers.length;

 

    for(var i=0;i<layerNum;i++){

        docRef.activeLayer = docRef.layers[i];

        if(!docRef.activeLayer.isBackgroundLayer){

            try{addMask ()}

            catch(e){}

            }

        }

    }

else{alert('There are no open files')};

 

function addMask(){

    var idMk = charIDToTypeID( "Mk  " );

        var desc2 = new ActionDescriptor();

        var idNw = charIDToTypeID( "Nw  " );

        var idChnl = charIDToTypeID( "Chnl" );

        desc2.putClass( idNw, idChnl );

        var idAt = charIDToTypeID( "At  " );

            var ref1 = new ActionReference();

            var idChnl = charIDToTypeID( "Chnl" );

            var idChnl = charIDToTypeID( "Chnl" );

            var idMsk = charIDToTypeID( "Msk " );

            ref1.putEnumerated( idChnl, idChnl, idMsk );

        desc2.putReference( idAt, ref1 );

        var idUsng = charIDToTypeID( "Usng" );

        var idUsrM = charIDToTypeID( "UsrM" );

        var idHdAl = charIDToTypeID( "HdAl" );

        desc2.putEnumerated( idUsng, idUsrM, idHdAl );

    executeAction( idMk, desc2, DialogModes.NO );

    }

olo_oloAuthor
Participant
April 3, 2014

Thanks so much csuebele. Your exact script was putting a layer mask on every layer.

I brought it into ExtendScript Toolkit and changed line 7

for(var i=0;i<layerNum;i++){

to this:

for(var i=0;i<layerNum-1;i++){

This did the trick as far as layer masking...do you know how to add the save as function into the script? I now know that I can select the correct layer using docRef.activeLayer = docRef.layers[layerNum-1]; but I don't know how to implement saving using that layer name. I'll start digging through the scripting guide now...

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
April 3, 2014

Yes your change is good.  That way the last layer won't have a mask applied.  I wasn't sure if you wanted that, but put in the check for the background layer. 

As far as saving, what file format do you want to save to?  Here's how I save a psd:

var docRef = activeDocument

var doneFolder = new Folder('/c/photos/')

var layerName = docRef.activeLayer.name

var psdOptions = new PhotoshopSaveOptions();

psdOptions.layers = true;

app.displayDialogs = DialogModes.ALL;//include this line if you want the dialog box to come up.

docRef.saveAs (new File(doneFolder +'/' + layerName + '.psd'), psdOptions);

app.displayDialogs = DialogModes.NO;//Resets the ALL - you don't want to leave that on.