Skip to main content
Shootermac
Known Participant
April 30, 2020
Answered

Apply Layer Mask to Numerous Layers (without grouping)

  • April 30, 2020
  • 7 replies
  • 8307 views

Hi all,

For reasons that I don't wish to bore people with, I am trying to apply the same layer mask over many layers - far too many to 'alt-drag'. Then later disable/delete them.

 

I've tried several work arounds, but not much luck. I can't select all layers and paste, or 'add layer mask' to multiple layers. The best I can do is load selcetion, but it wont cut all layers. 

I tried to do an action, but I haven't done it in years. I can action it layer at a time, but if I work over a few layers, I get the - The Command "Make" is not currently available. 

I'm an intemittent user, so things may have changed for me. 

Thanks for your help.

 

G

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Hi,
Thanks for that. 


When I have fresher eyes tomorrow morning, I'll take a better look. 

Regarding your last, no particular reason, like I said, I use it enough to know how much of the app I don't know.

Thanks again. 


// apply active selection as layer masks to selected layers;
// 2020, use it at your own risk;
if(app.documents.length != 0) {
applyLayerMasksToLayers();
};
////// apply layer masks to selected layers //////
function applyLayerMasksToLayers(){
var theLayers = getSelectedLayersIdx();
var selection = hasSelection();
if (selection == true) {app.activeDocument.selection.deselect()};
for(i = 0; i < theLayers.length; i++){
selectLayerByIndex(theLayers[i], false);
// remove existing mask;
if (hasLayerMask (theLayers[i]) == true) {
var desc8 = new ActionDescriptor();
var ref5 = new ActionReference();
var idchannel = stringIDToTypeID( "channel" );
ref5.putEnumerated( idchannel, idchannel, stringIDToTypeID( "mask" ) );
desc8.putReference( stringIDToTypeID( "null" ), ref5 );
executeAction( stringIDToTypeID( "delete" ), desc8, DialogModes.NO );
};
// load selection;
   reselect ();
// apply layer mask;
if (selection == true) {makeLayerMask('RvlS')}
else {makeLayerMask('RvlA')};
}
};
////// by paul mr //////
function getSelectedLayersIdx(){ 
    var selectedLayers = new Array; 
    var ref = new ActionReference(); 
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
    var desc = executeActionGet(ref); 
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ 
       desc = desc.getList( stringIDToTypeID( 'targetLayers' )); 
        var c = desc.count 
        var selectedLayers = new Array(); 
        for(var i=0;i<c;i++){ 
          try{ 
             activeDocument.backgroundLayer; 
             selectedLayers.push(  desc.getReference( i ).getIndex() ); 
          }catch(e){ 
             selectedLayers.push(  desc.getReference( i ).getIndex()+1 ); 
          } 
        } 
     }else{ 
       var ref = new ActionReference(); 
       ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
       ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
       try{ 
          activeDocument.backgroundLayer; 
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1); 
       }catch(e){ 
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))); 
       } 
    } 
    return selectedLayers; 
};
////// by mike hale, via paul riggott //////
function selectLayerByIndex(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
  desc.putReference(charIDToTypeID("null"), ref );
     if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
    desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
 try{
  executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
////// add layer mask //////
function makeLayerMask(maskType) {
try {
if( maskType == undefined) maskType = 'RvlA' ; //from selection
// =======================================================
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 ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
desc3.putReference( idAt, ref1 );
var idUsng = charIDToTypeID( "Usng" );
var idUsrM = charIDToTypeID( "UsrM" );
var idRvlA = charIDToTypeID(maskType);
desc3.putEnumerated( idUsng, idUsrM, idRvlA );
executeAction( idMk, desc3, DialogModes.NO );
} catch (e) {return false};
};
////// check for selection //////
function hasSelection(){
var ref10 = new ActionReference();
ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
ref10.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var docDesc = executeActionGet(ref10);
return docDesc.hasKey(stringIDToTypeID("selection"));
};
////// reselect //////
function reselect () {
try {
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref2.putProperty( idChnl, idfsel );
desc7.putReference( idnull, ref2 );
var idT = charIDToTypeID( "T   " );
var idOrdn = charIDToTypeID( "Ordn" );
var idPrvs = charIDToTypeID( "Prvs" );
desc7.putEnumerated( idT, idOrdn, idPrvs );
executeAction( idsetd, desc7, DialogModes.NO );
} catch (e) {return false}
};
////// has layer mask //////
function hasLayerMask (theIndex) {  
var m_Dsc01, m_Ref01;  
m_Ref01 = new ActionReference();  
m_Ref01.putIndex(charIDToTypeID("Lyr "), theIndex);  
//m_Ref01.putEnumerated(sTID("layer"), cTID("Ordn"), cTID("Trgt"));  
m_Dsc01 = executeActionGet(m_Ref01);  
return m_Dsc01.hasKey(charIDToTypeID("Usrs"));  
};

7 replies

Stephen Marsh
Community Expert
July 5, 2021

And this one:

 

 

// https://forums.adobe.com/message/8895442#8895442
// Loop or iterate an action to multiple layers
// You need to edit the code to include the name of your action that you created (line 26) and the name of the group folder that the action is in (line 28)
#target photoshop  
  
var doc = app.activeDocument;  
var layerVisble  
  
doc.activeLayer = doc.layers[0];  
  
for(var i=0;i<doc.layers.length;i++){  
    doc.activeLayer = doc.layers[i];  
    layerVisible = doc.activeLayer.visible;  
    doc.activeLayer.visible = true;  
    spot ();  
    doc.activeLayer.visible = layerVisible;  
    }  
alert('done');  
  
function spot(){  
    var idPly = charIDToTypeID( "Ply " );  
    var desc2 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
    var ref1 = new ActionReference();  
    var idActn = charIDToTypeID( "Actn" );  
    ref1.putName( idActn, "TEMP" );  // Name of action  
    var idASet = charIDToTypeID( "ASet" );  
    ref1.putName( idASet, "TEMP" ); // Action set name  
    desc2.putReference( idnull, ref1 );  
executeAction( idPly, desc2, DialogModes.NO );  
}

 

Or perhaps this one:

 

// https://forums.adobe.com/message/8895266#8895266
// https://forums.adobe.com/message/8895401#8895401
var ActionName = "RandonFillColor";  
var ActionSet = "temp";  
  
if (!documents.length) { alert('There are no documents open.', 'No Document');}  
else {processArtLayers(activeDocument);}  
  
function processArtLayers(obj) {  
    for( var i = obj.artLayers.length-1; 0 <= i; i--) { activeDocument.activeLayer = obj.artLayers[i]; doAction(ActionName, ActionSet);}  
    for( var i = obj.layerSets.length-1; 0 <= i; i--) {processArtLayers(obj.layerSets[i]); } // Process Layer Set Layers  
}  

 

Known Participant
July 5, 2021

Thank you, Stephen!

Stephen Marsh
Community Expert
July 5, 2021

And this one:

 

// https://forums.adobe.com/thread/2285411
// Change Line 15 for the action and parent action set
#target photoshop  
if (app.documents.length > 0) {  
var myDocument = app.activeDocument;  
myDocument.suspendHistory("operation", "main(myDocument)");  
};  
////////////////////////////////////  
function main () {  
var theLayers = getSelectedLayersIdx();  
// do stuff;  
// reselect layers;  
for (var p = 0; p < theLayers.length; p++) {  
  selectLayerByIndex(theLayers[p], false);  
  app.doAction('Invert Action', 'Invert Set'); // Define the action to play and the action set 
  };  
};  
//   
function selectLayerByIndex(index,add){  
add = undefined ? add = false:add  
var ref = new ActionReference();  
    ref.putIndex(charIDToTypeID("Lyr "), index);  
    var desc = new ActionDescriptor();  
    desc.putReference(charIDToTypeID("null"), ref );  
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );  
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );  
   try{  
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );  
}catch(e){  
alert(e.message);  
}  
};  
////// by paul mr;  
function getSelectedLayersIdx(){  
      var selectedLayers = new Array;  
      var ref = new ActionReference();  
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
      var desc = executeActionGet(ref);  
      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){  
         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));  
          var c = desc.count  
          var selectedLayers = new Array();  
          for(var i=0;i<c;i++){  
            try{  
               activeDocument.backgroundLayer;  
               selectedLayers.push(  desc.getReference( i ).getIndex() );  
            }catch(e){  
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );  
            }  
          }  
       }else{  
         var ref = new ActionReference();  
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));  
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
         try{  
            activeDocument.backgroundLayer;  
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);  
         }catch(e){  
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));  
         }  
      }  
      return selectedLayers;  
};
Known Participant
July 5, 2021

This works perfect for what I need, and applies my action on selected layers.

Stephen Marsh
Community Expert
May 8, 2020

The following script may not be perfect, however, it will get the job done.  It is more of a "general purpose" script to apply "something" to all top-level layers and layer sets and also to layers within layer sets. One could obviously swap out the function to run an action with "use-specific" script code so that there is no dependency on having an action. I decided to use an action for the "use-specific" processing in this topic as this makes the script accessible and modular.

 

The script does all the heavy lifting, however, it requires the following to be set before running the script:

 

1) The mask channel should be copied to the clipboard

 

2) An action that applies the clipboard content as a layer mask with an action set name of Copied Channel as Mask with a referenced action name of Run

 

 

Link to action:

Copied Channel as Mask.atn

 

 

 

/* 

Apply layer mask action to all layer sets and layers.jsx

In reply to:
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

The script does all the heavy lifting, however, it requires the following to be set before running the script:

1) The mask channel should be copied to the clipboard
2) An action that applies the clipboard content as a layer mask with an action set name of 'Copied Channel as Mask' with a referenced action name of 'Run'

Based on:
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.layers" to "obj.artLayers" to exclude layer sets
        for (var al = obj.layers.length - 1; 0 <= al; al--) {
            app.activeDocument.activeLayer = obj.layers[al];

            // Start function call to run action
            runAction();
            // Finish function call to run action

        }

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

            // Start function call to run action
            runAction();
            // Finish function call to run action

        }
    }

    function runAction() {
        app.doAction('Run', 'Copied Channel as Mask'); // Action and action set name
    }
}

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

 

 

 

Stephen Marsh
Community Expert
July 5, 2021

Another option:

 

Apply Action To Layers.jsx

 

 

PECourtejoie
Community Expert
April 30, 2020

You could also place the layers inside a clipping group, it is not exactly a layer group...

Shootermac
Known Participant
May 1, 2020

Hi,

I should have added, say I'm doing a profile photo of a car, in 50 plus scenerios, I wish to align the cars, but not let other things in the frame distract it, clouds, treees, etc.

When I did it as a group, I felt that when I did it as a group mask, it the intelligence was still influenced by the whole picture, not the isolated part (masked car). I'm not sure with clipping group would act any different, or enable me to bring back the BGs (I'll google clipping groups).

Thanks

Stephen Marsh
Community Expert
May 7, 2020

Photoshop is a visual tool, however, the forum only has text descriptions to go by, which can be hard. If a picture is worth a thousand words, wouldn't a screenshot of your layers, or samples of the image layer stack elements be helpful?

Shootermac
Known Participant
April 30, 2020

Hi,

Yes, that's what I mean by being an intermittent user. I'm sure in the past I would end the action by selecting the next layer. 

I had a selection saved. I had a layer selected. 1, I created a new set, then a new action. 2, layer, layer mask, hide all. 3, load selection. 4, fill white. 5, deselect. 6, select next layer. 7, stop.

I'm sure it's gotten more complicated than that. Any advice to point me in the right direction would help. 

Cheers,


c.pfaffenbichler
Community Expert
April 30, 2020

»1, I created a new set, then a new action.

2, layer, layer mask, hide all.

3, load selection.

4, fill white.

5, deselect.

6, select next layer.

7, stop.«

Why do you add a Layer Mask, then load the Selection and fill instead of loading the Selection first and applying the Layer Mask then? 

Shootermac
Known Participant
May 7, 2020
// apply active selection as layer masks to selected layers;
// 2020, use it at your own risk;
if(app.documents.length != 0) {
applyLayerMasksToLayers();
};
////// apply layer masks to selected layers //////
function applyLayerMasksToLayers(){
var theLayers = getSelectedLayersIdx();
var selection = hasSelection();
if (selection == true) {app.activeDocument.selection.deselect()};
for(i = 0; i < theLayers.length; i++){
selectLayerByIndex(theLayers[i], false);
// remove existing mask;
if (hasLayerMask (theLayers[i]) == true) {
var desc8 = new ActionDescriptor();
var ref5 = new ActionReference();
var idchannel = stringIDToTypeID( "channel" );
ref5.putEnumerated( idchannel, idchannel, stringIDToTypeID( "mask" ) );
desc8.putReference( stringIDToTypeID( "null" ), ref5 );
executeAction( stringIDToTypeID( "delete" ), desc8, DialogModes.NO );
};
// load selection;
   reselect ();
// apply layer mask;
if (selection == true) {makeLayerMask('RvlS')}
else {makeLayerMask('RvlA')};
}
};
////// by paul mr //////
function getSelectedLayersIdx(){ 
    var selectedLayers = new Array; 
    var ref = new ActionReference(); 
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
    var desc = executeActionGet(ref); 
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ 
       desc = desc.getList( stringIDToTypeID( 'targetLayers' )); 
        var c = desc.count 
        var selectedLayers = new Array(); 
        for(var i=0;i<c;i++){ 
          try{ 
             activeDocument.backgroundLayer; 
             selectedLayers.push(  desc.getReference( i ).getIndex() ); 
          }catch(e){ 
             selectedLayers.push(  desc.getReference( i ).getIndex()+1 ); 
          } 
        } 
     }else{ 
       var ref = new ActionReference(); 
       ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
       ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
       try{ 
          activeDocument.backgroundLayer; 
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1); 
       }catch(e){ 
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))); 
       } 
    } 
    return selectedLayers; 
};
////// by mike hale, via paul riggott //////
function selectLayerByIndex(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
  desc.putReference(charIDToTypeID("null"), ref );
     if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
    desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
 try{
  executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
////// add layer mask //////
function makeLayerMask(maskType) {
try {
if( maskType == undefined) maskType = 'RvlA' ; //from selection
// =======================================================
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 ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idMsk = charIDToTypeID( "Msk " );
ref1.putEnumerated( idChnl, idChnl, idMsk );
desc3.putReference( idAt, ref1 );
var idUsng = charIDToTypeID( "Usng" );
var idUsrM = charIDToTypeID( "UsrM" );
var idRvlA = charIDToTypeID(maskType);
desc3.putEnumerated( idUsng, idUsrM, idRvlA );
executeAction( idMk, desc3, DialogModes.NO );
} catch (e) {return false};
};
////// check for selection //////
function hasSelection(){
var ref10 = new ActionReference();
ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
ref10.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var docDesc = executeActionGet(ref10);
return docDesc.hasKey(stringIDToTypeID("selection"));
};
////// reselect //////
function reselect () {
try {
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref2.putProperty( idChnl, idfsel );
desc7.putReference( idnull, ref2 );
var idT = charIDToTypeID( "T   " );
var idOrdn = charIDToTypeID( "Ordn" );
var idPrvs = charIDToTypeID( "Prvs" );
desc7.putEnumerated( idT, idOrdn, idPrvs );
executeAction( idsetd, desc7, DialogModes.NO );
} catch (e) {return false}
};
////// has layer mask //////
function hasLayerMask (theIndex) {  
var m_Dsc01, m_Ref01;  
m_Ref01 = new ActionReference();  
m_Ref01.putIndex(charIDToTypeID("Lyr "), theIndex);  
//m_Ref01.putEnumerated(sTID("layer"), cTID("Ordn"), cTID("Trgt"));  
m_Dsc01 = executeActionGet(m_Ref01);  
return m_Dsc01.hasKey(charIDToTypeID("Usrs"));  
};

Hi,

Looks impressive, but I dont know what that means or how to use it.
Thanks,

G

 

c.pfaffenbichler
Community Expert
April 30, 2020

I expect this would need a Script. 

How can the Layers that you need to apply the Layer Mask to be identified? 

JJMack
Community Expert
April 30, 2020

Post your action? How does your action know which layers to add the layer mask to?

JJMack