Action To Add A Layer Mask To ALL Layers?

Explorer ,
Feb 03, 2017 Feb 03, 2017

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

5.0K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Explorer , Feb 04, 2017 Feb 04, 2017
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 LayersNext thing was to add a step in the action which selects the TOP layer (this was important for some reason - without it,...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 03, 2017 Feb 03, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2017 Feb 04, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 04, 2017 Feb 04, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2017 Feb 04, 2017

Copy link to clipboard

Copied

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!!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 04, 2017 Feb 04, 2017

Copy link to clipboard

Copied

Congrats for making it work!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 26, 2019 Oct 26, 2019

Copy link to clipboard

Copied

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 );
    }

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2020 Dec 19, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 19, 2020 Dec 19, 2020

Copy link to clipboard

Copied

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.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

It has been a long time, 30+years since I worked with coding etc and some assistance would be greatly appreciated. 🙂  I would like to use that script to apply HideAll masks to all my layers except the background. What exactly do I need to remove/alter from the script above and what exactly to copy/paste to the jsx file?

I copy/pasted the complete text above except for the changes in the following lines, restarted photoshop, ran the script but nothing happened?

 

var idHdAl = charIDToTypeID( "HdAl" );
desc2.putEnumerated( idHdAl );
executeAction( idMk, desc2, DialogModes.NO );
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

Sorry, I couldn't figure out how to edit my post... I have four groups each with multiple layers and I just noticed that the script applies masks to each group but not the individual layers within the group.  I am doing this for colorization so a group mask won't work for me. Thanks in advance - Stay Safe!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

Don't bother responding as I have a work around for now - Cheers! Sorry for any inconvenience.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

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()');

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

Is it possible to do this on selected layers aswell?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

LATEST

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines