Skip to main content
Inspiring
February 3, 2017
Answered

Action To Add A Layer Mask To ALL Layers?

  • February 3, 2017
  • 3 replies
  • 18347 views

Is it possible to make an action to add a (black) layer mask to ALL layers?

 

I know how to select all layers.

 

After I have selected them, the Layer Mask option is Greyed Out though under the Layer menu (oddly enough, I can apply an adjustment layer to ALL layers, but not a layer mask!)

 

Unfortunately, putting the layers into a group and then masking the entire group will not work for me.

 

Is there a way that photoshop can "step through" the different layers and mask them one at a time at least?

 

I usually have somewhere between 4 to 10 layers per file (it varies from file to file), and it is a bit tedious doing this by hand on every layer.

 

Thanks in advance.

This topic has been closed for replies.
Correct answer Easy Mark

Yes, your example is how you would do it -- set up the number of layers you want, press record and add a layer mask to each layer one at a time, then stop. If you run the action on a file with fewer than the 5 layers in your example, a message will appear and you can either click Continue or Stop.

There is a a post, a couple of years old now, that presents a script for doing what you want:

Script to add layer mask to all layers but background?

So you might give that a whirl.


Thank you again for the response, Barbara. With your help, I figured out how to do it!!!

I used one of the scripts in the post to which you linked, and it works fine.

Now, I wanted to first Align all the layers (it is kind of like an HDR image where there are several frames at different exposures), so I created an action, used the Select All Layers, then Edit -> Auto Align Layers

Next thing was to add a step in the action which selects the TOP layer (this was important for some reason - without it, the masking script didn't work). So - while still recording - press your alt and your . keys at the same time (I guess that is option + . on a Mac)

Then final step was to click the flyout menu of the action pallet, select the Insert Menu Item, and then navigate to the script that will mask all the layers.

That middle part where you have to select the top layer needs to be run otherwise it won't mask the top layer.

Thanks again. I am a happy camper!!!

3 replies

Stephen Marsh
Community Expert
Community Expert
March 25, 2021

I just hacked a slightly different script just in case anybody else is looking for an updated solution for 2021.

 

 

 

/* 

Apply hide all layer mask to all layers & nested layers.jsx
Original script modified by Stephen Marsh - 2021

In reply to:
Action To Add A Layer Mask To ALL Layers
https://community.adobe.com/t5/photoshop/action-to-add-a-layer-mask-to-all-layers/m-p/11923940

Based on:
Apply Layer Mask to Numerous Layers (without grouping)
https://community.adobe.com/t5/photoshop/apply-layer-mask-to-numerous-layers-without-grouping/td-p/11093194

https://forums.adobe.com/message/8895266#8895266
https://forums.adobe.com/message/8895401#8895401

*/

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
    } else {
        processAllLayersAndSets(app.activeDocument);
    }

    function processAllLayersAndSets(obj) {

        // Process all layers and layer sets
        // Change the following 2 entries of "obj.artLayers" to "obj.layers" to include layer sets
        for (var al = obj.artLayers.length - 1; 0 <= al; al--) {
            app.activeDocument.activeLayer = obj.artLayers[al];

            addMask("hideAll");

        }

        // Process Layer Set Layers 
        for (var ls = obj.layerSets.length - 1; 0 <= ls; ls--) {
            processAllLayersAndSets(obj.layerSets[ls]);

            addMask("hideAll");

        }
    }

    function addMask(maskVisibility) {

        // maskVisibility = "revealAll" or "hideAll"

        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        descriptor.putClass(s2t("new"), s2t("channel"));
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("mask"));
        descriptor.putReference(s2t("at"), reference);
        descriptor.putEnumerated(s2t("using"), c2t("UsrM"), s2t(maskVisibility));
        executeAction(s2t("make"), descriptor, DialogModes.NO);
    }

}

activeDocument.suspendHistory('Process Layers & Sets', 'main()');

 

 

Known Participant
July 5, 2021

Is it possible to do this on selected layers aswell?

Kukurykus
Legend
July 5, 2021
Stephen Marsh
Community Expert
Community Expert
October 26, 2019

The script link in Barbara's post no longer works. For those looking for a similar script:

 

 

// RevealMask-AllLayers – Adds a 'Reveal All' mask to all layers
//graphicdesign.stackexchange.com/questions/94201/automate-adding-individual-reveal-all-mask-to-many-layers-in-photoshop

#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( "RvlA" ); // or to hide all: HdAl
        desc2.putEnumerated( idUsng, idUsrM, idHdAl ); // or to hide all: idHdAl
    executeAction( idMk, desc2, DialogModes.NO );
    }

 

Participant
December 19, 2020

This script is not compatible with Photoshop 2021.  Can anyone update it or give a link to a version to that is compatible.

Stephen Marsh
Community Expert
Community Expert
December 20, 2020

Is there an error reported? If so please post a cropped screenshot. It may also help to post a cropped screenshot of your expanded layers panel.

 

barbara_a7746676
Community Expert
Community Expert
February 3, 2017

Layer masks must be added to layers one layer at a time. However, you could create an action that adds a layer mask to each layer one at a time. If the files you're working with all have the same number of layers, the action will complete successfully. If each file has a different number of layers, a message will appear when running the action, and you'll need to either click 'Continue' or 'Stop'.

If you want to have an identical layer mask on all layers, you could first group the layers and have an action that adds a layer mask to the whole group.

Easy MarkAuthor
Inspiring
February 4, 2017

Thank you for your response, Barbara:

Unfortunately, grouping layers won't work for my needs. I end up blending DIFFERENT PARTS of different layers to make up the final image.

Could you explain to me how to "create an action that adds a layer mask to each layer one at a time"? I know how to record an action. But after pressing the record button, how would I have it cycle through the layers?

Should I just create a document with - for example - five layers - and press the record button and then add a layer mask to those five layers, and then hit the stop button?

Thanks in advance.

And if you have any influence on the powers that be, could you communicate to them that having the ability to apply a layer mask to ALL layers would be appreciated, since it seems it is possible to apply an adjustment layer to all layers without having to group them.

barbara_a7746676
Community Expert
Community Expert
February 4, 2017

Yes, your example is how you would do it -- set up the number of layers you want, press record and add a layer mask to each layer one at a time, then stop. If you run the action on a file with fewer than the 5 layers in your example, a message will appear and you can either click Continue or Stop.

There is a a post, a couple of years old now, that presents a script for doing what you want:

Script to add layer mask to all layers but background?

So you might give that a whirl.