Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add same mask to multiple layers

New Here ,
Jan 18, 2021 Jan 18, 2021

This is my first post so first of all I would like to say Hi to all of you wonderful people!

 

I have a problem that I hope someone of you will help resolve.

Let me explain the situation.

 

I have 108 layers to which I want to apply a mask that I've prepared earlier (same mask to every single layer).

 

After that I want to export these layers as separate images (JPG) with a certain background color (F8F8F8).

 

Is there a way to automate it?

Dont want complain too much but I have 1500 images in one

set that I did manually and I feel like a robot after a while of working like that...

 

Would appreciate your support!

 

best,

Lukas

 

TOPICS
Actions and scripting
788
Translate
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 Expert ,
Jan 18, 2021 Jan 18, 2021

Group all the layers  mask the group

JJMack
Translate
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 ,
Jan 18, 2021 Jan 18, 2021

Unfortunately this does not work with "export as"

Translate
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 ,
Jan 18, 2021 Jan 18, 2021

... also I need a specific background color for all the layers.
oh and by the way... thank you for the reply JJMack

Translate
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
LEGEND ,
Jan 18, 2021 Jan 18, 2021

It would export layers without mask.

Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021

JJMack's suggestion is still valid, it just needs a script to automate the saving...

 

If you put all the image layers into a group and mask that group/set/folder.

 

Then make a second set/group/folder with only the static background colour in it.

 

Then you can use the following script:

 

https://github.com/mechanicious/photoshopCompositionComposer

 

layers.png

 

result.png

 

https://community.adobe.com/t5/photoshop/saving-multiple-files-into-same-folder-from-actions/td-p/11...

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021

Another option is to record the creation of making the mask into an action, then use a script to play the action over selected/all layers.

 

//	I pretty much copy-pasted everything from NinjaScript's script,
//	added couple funtions from "ES-Collection/Photoshop-Scripts/Remove Unused Layers.jsx" on GitHub,
//	and edited it all to work in Photoshop 2020 :)
//	Also added option to choose what type of layers you applying to.

#target photoshop

var scriptName = "Apply Action To Layers";
var scriptCreator = "Originally made by NinjaScript"
function cID (inVal) { return charIDToTypeID(inVal);}
function sID (inVal) { return stringIDToTypeID(inVal);}
var currentActionSets = getActionSets();
main();

function main()
{
    app.bringToFront();
    optionsDialog();
}

function optionsDialog()
{
    var ButtonWidth = 110;

	OpenOptionsDialog = new Window("dialog", scriptName + "...  " + scriptCreator);
	OpenOptionsDialog.orientation = 'column';
	OpenOptionsDialog.alignChildren = 'left';
    mainGroup = OpenOptionsDialog.add("group");
    mainGroup.orientation = 'column';
    mainGroup.alignChildren = 'left';
    mainGroup.alignment = 'left';
	
//Group	
    var actionSetGroup = mainGroup.add("group");
    actionSetGroup.orientation = 'row';
    actionSetGroup.add("statictext",undefined, "Action Set:")
    var DDActionSet = actionSetGroup.add("dropdownlist",undefined, "")
    DDActionSet.preferredSize.width = 210;
       
    for (var i = 0; i < currentActionSets.length; i++)
    {
		DDActionSet.add("item", currentActionSets[i]);
    }
	DDActionSet.selection = 0;
	for (var i = 0; i < currentActionSets.length; i++){	//
		if(currentActionSets[i] == "Mine"){				//	Selects by default Action Set with name "Mine", instead of 1st by alphabetical
			DDActionSet.selection = i;					//
		}												//
	}													//
//   
    
//Action
    var actionGroup = mainGroup.add("group");
    actionGroup.orientation = 'row';
    actionGroup.add("statictext",undefined, "Action:      ")
    DDActions = actionGroup.add("dropdownlist",undefined, "")
    DDActions.preferredSize.width = 210;
    
    function populateDDActions (inSet)
    {
        DDActions.removeAll();        
        for (var i = 0; i < currentActionSets[inSet].actions.length; i++)
        {
            DDActions.add("item", currentActionSets[inSet].actions[i]);
        }
        DDActions.selection = 0;
    }
    DDActionSet.onChange = function()
    {
        populateDDActions(DDActionSet.selection.index);
    }
    DDActionSet.onChange();
//

//ApplyTo
    var ApplyTo = mainGroup.add("group");
    ApplyTo.orientation = 'row';
    ApplyTo.add("statictext",undefined, "Apply To:  ")
    DDApplyTo = ApplyTo.add("dropdownlist",undefined, "")
    DDApplyTo.preferredSize.width = 110;
    DDApplyTo.removeAll();
	DDApplyTo.add("item", "Selected Layers");
	DDApplyTo.add("item", "Visible Layers");
	DDApplyTo.add("item", "ALL Layers");
	DDApplyTo.selection = 0;
//

//Run
    mainGroup.add("statictext", undefined, "");
     
    ButtonGroup = mainGroup.add("group");
    ButtonGroup.orientation = 'row';
    ButtonGroup.alignChildren = 'center';
    ButtonGroup.alignment = 'top';     
            
    buttonRun= ButtonGroup.add("button",undefined, "Run")
    buttonRun.preferredSize.width = ButtonWidth;
    buttonRun.onClick = function()
   {
		if(DDApplyTo.selection == 0)	//ToSelected
		{
			AllSelected = getSelectedLayersIdx();
			var doc = app.activeDocument;
			for(var i in AllSelected)
			{
				if(isLayerSet(Number(AllSelected[i])) == 0 && isLocked(Number(AllSelected[i])) == 0 && isLayerSetEnd(Number(AllSelected[i])) == 0 && isAdjustmentLayer(Number(AllSelected[i])) == 0 && isClippingLayer(Number(AllSelected[i])) != 'topClippingLayer' && isClippingLayer(Number(AllSelected[i])) != 'middleClippingLayer')
				{
					deselectLayers();
					selectLayerByIndex(Number(AllSelected[i]),true);
					var v = getLayerVisibilityByIndex(Number(AllSelected[i]));
					doc.activeLayer.visible = true;
					app.doAction(DDActions.selection.text, DDActionSet.selection.text);
					if(v == 0)
					{
						doc.activeLayer.visible = false;
					}
				}
			}
			deselectLayers();
			OpenOptionsDialog.close();
		}
		if(DDApplyTo.selection == 1)	//ToVisible
		{
			var doc = app.activeDocument;
			selectAllLayers();
			var layersSelected=getSelectedLayersIdx();
			var u = 0;
			var w = layersSelected.length;
			deselectLayers();
			for(var i = 1; i < w; i++)
			{
				if(isLayerSet(i))
				{
					w++;
					u++;					
				}
			}
			selectAllLayers();
			for(i = layersSelected.length + u; i > 0; i--)
			{
				if(isLayerSet(i) == 0 && isLocked(i) == 0 && isLayerSetEnd(i) == 0 && getLayerVisibilityByIndex(i) == 1 && isAdjustmentLayer(i) == 0 && isClippingLayer(i) != 'topClippingLayer' && isClippingLayer(i) != 'middleClippingLayer')
				{
					deselectLayers();
					selectLayerByIndex(i,true);
					var v = getLayerVisibilityByIndex(i);
					doc.activeLayer.visible = true;
					app.doAction(DDActions.selection.text, DDActionSet.selection.text);
					if(v == 0)
					{
						doc.activeLayer.visible = false;
					}
				}
			}
			deselectLayers();
			OpenOptionsDialog.close();			
		}		
		if(DDApplyTo.selection == 2)	//ToAll
		{
			var doc = app.activeDocument;
			selectAllLayers();
			var layersSelected=getSelectedLayersIdx();
			var u = 0;
			var w = layersSelected.length;
			deselectLayers();
			for(var i = 1; i < w; i++)
			{
				if(isLayerSet(i))
				{
					w++;
					u++;					
				}
			}
			selectAllLayers();
			for(i = layersSelected.length + u; i > 0; i--)
			{
				if(isLayerSet(i) == 0 && isLocked(i) == 0 && isLayerSetEnd(i) == 0 && isAdjustmentLayer(i) == 0 && isClippingLayer(i) != 'topClippingLayer' && isClippingLayer(i) != 'middleClippingLayer')
				{
					deselectLayers();
					selectLayerByIndex(i,true);
					var v = getLayerVisibilityByIndex(i);
					doc.activeLayer.visible = true;
					app.doAction(DDActions.selection.text, DDActionSet.selection.text);
					if(v == 0)
					{
						doc.activeLayer.visible = false;
					}
				}
			}
			deselectLayers();
			OpenOptionsDialog.close();			
		}
    }
//

//Exit    
    buttonClose= ButtonGroup.add("button",undefined, "Exit")
    buttonClose.preferredSize.width = ButtonWidth;
    buttonClose.onClick = function() {OpenOptionsDialog.close()}       
    //Show window
  OpenOptionsDialog.center();
  var result = OpenOptionsDialog.show();
}

function getActionSets() 
{  
  var i = 1;  
  var sets = [];  
  while (true) {  
    var ref = new ActionReference();  
    ref.putIndex(cID("ASet"), i);  
    var desc;  
    var lvl = $.level;  
    $.level = 0;  
    try {  
      desc = executeActionGet(ref);  
    } catch (e) {  
      break;    // all done  
    } finally {  
      $.level = lvl;  
    }  
    if (desc.hasKey(cID("Nm  "))) {  
      var set = {};  
      set.index = i;  
      set.name = desc.getString(cID("Nm  "));  
      set.toString = function() { return this.name; };  
      set.count = desc.getInteger(cID("NmbC"));  
      set.actions = [];  
      for (var j = 1; j <= set.count; j++) {  
        var ref = new ActionReference();  
        ref.putIndex(cID('Actn'), j);  
        ref.putIndex(cID('ASet'), set.index);  
        var adesc = executeActionGet(ref);  
        var actName = adesc.getString(cID('Nm  '));  
        set.actions.push(actName);  
      }  
      sets.push(set);  
    }  
    i++;  
  }  
  return sets;  
}  

function deselectLayers() {
    var desc01 = new ActionDescriptor();
        var ref01 = new ActionReference();
        ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc01.putReference( charIDToTypeID('null'), ref01 );
    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO );
}

function selectLayerByIndex(index,add){
    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){}
}

function getLayerVisibilityByIndex( idx ) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Vsbl" ));
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    return executeActionGet(ref).getBoolean(charIDToTypeID( "Vsbl" ));
}

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

function isLayerSet(idx){
   var propName = stringIDToTypeID( 'layerSection' );// can't replace
   var ref = new ActionReference();
   ref.putProperty( 1349677170 , propName);
   ref.putIndex( 1283027488, idx );
   var desc =  executeActionGet( ref );
   var type = desc.getEnumerationValue( propName );
   var res = typeIDToStringID( type );
   return res == 'layerSectionStart' ? true:false;
}

function isLayerSetEnd(idx){
   var propName = stringIDToTypeID( 'layerSection' );// can't replace
   var ref = new ActionReference();
   ref.putProperty( 1349677170 , propName);
   ref.putIndex( 1283027488, idx );
   var desc =  executeActionGet( ref );
   var type = desc.getEnumerationValue( propName );
   var res = typeIDToStringID( type );
   return res == 'layerSectionEnd' ? true:false;
}

function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}

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

function isLocked(myLayer){
    selectLayerByIndex(myLayer);

    if(activeDocument.activeLayer.allLocked || activeDocument.activeLayer.pixelsLocked || activeDocument.activeLayer.positionLocked || activeDocument.activeLayer.transparentPixelsLocked){
        return true;
    }
    return false;
}

function isAdjustmentLayer(){
	var ref = new ActionReference();
	ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
	return executeActionGet(ref).hasKey(stringIDToTypeID('adjustment'));
}

function isClippingLayer(layerID){
	var clipInfo=false;

	var ref = new ActionReference();
	    ref.putIndex(charIDToTypeID("Lyr "), layerID);

	try{
		var desc = executeActionGet(ref);
	} catch(e) {
		// Not a valid layer
		return clipInfo;
	}

	var group = desc.getBoolean(stringIDToTypeID('group'));
	if(group) clipInfo = 'topClippingLayer';

	try{
   		var ref = new ActionReference();
   		ref.putIndex(charIDToTypeID( 'Lyr ' ), layerID+1 );
   		desc =  executeActionGet(ref);
	}catch(e){
		//alert("Top layer!");
		return clipInfo;
	}

    group = desc.getBoolean(stringIDToTypeID('group'));
    if(group && clipInfo == 'topClippingLayer' ) clipInfo = 'middleClippingLayer';
    if(group && clipInfo == false ) clipInfo = 'bottomClippingLayer';
    return clipInfo;
};

 

Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021

Or this script to add a mask to all layers based from a selection.

 

/* 
Apply Layer Mask to Numerous Layers(without grouping)
https://community.adobe.com/t5/photoshop/apply-layer-mask-to-numerous-layers-without-grouping/m-p/11115417

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

 

 

Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021

And another one using an action:

 

/* 

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

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'); // Change action and set name
    }
}

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

 

Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021

And another mask action script:

 

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

 

Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021

And another 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 );  
}
Translate
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
Community Expert ,
Jan 18, 2021 Jan 18, 2021
LATEST

And one final script for now!

 

https://raw.githubusercontent.com/Paul-Riggott/PS-Scripts/master/Run%20Action.jsx

 

#target photoshop
app.bringToFront();

main();
function main(){
if(!documents.length){
    alert("You need to have at least one document open!");
    return;
}
var win = new Window( 'dialog', 'RA' ); 
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.orientation='stack';
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"}); 
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Run Action(s)');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.p1.p1= win.p1.add("panel", undefined, undefined, {borderStyle:"black"}); 
win.p1.p1.preferredSize=[300,2];
win.g3 =win.p1.add('group');
win.g3.orientation = "row";
win.g3.alignment='fill';
win.g3.spacing=10;
win.g3.cb1 = win.g3.add('checkbox',undefined,'Create Snapshop');
win.g5 =win.p1.add('group');
win.g5.orientation = "row";
win.g5.alignment='fill';
win.g5.spacing=10;
win.g5.cb1 = win.g5.add('checkbox',undefined,'Run Action(s) On All Layers');
win.g5.cb1.value=true;
win.g6 =win.p1.add('group');
win.g6.orientation = "row";
win.g6.alignment='fill';
win.g6.spacing=10;
win.g6.cb1 = win.g6.add('checkbox',undefined,'Run Action(s) On Selected Layer(s)');
if(activeDocument.layers.length < 2){
    win.g6.cb1.enabled=false;
    }
var selectedLayers= getSelectedLayersIdx();
if(selectedLayers.length <1)  win.g6.cb1.enabled=false;

win.g5.cb1.onClick=function(){
    if(win.g5.cb1.value){
        win.g6.cb1.value=false;
       }else{
            win.g6.cb1.value=true;
           if(selectedLayers.length >1)  win.g6.cb1.enabled=true;
           }
}
win.g6.cb1.onClick=function(){
    if(win.g6.cb1.value){
        win.g5.cb1.value=false;
       }else{
           win.g5.cb1.value=true;
           }
}
win.g5.cb1.onClick();
win.p1.p2= win.p1.add("panel", undefined, undefined, {borderStyle:"black"}); 
win.p1.p2.preferredSize=[300,2];
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.spacing=10;
win.g10.dd1 = win.g10.add('dropdownlist');
win.g10.dd2 = win.g10.add('dropdownlist');
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g15.spacing=10;
win.g15.st1 = win.g15.add('statictext',undefined,'Selected Actions');
win.g15.et1 = win.g15.add('edittext',undefined,'0');
win.g15.et1.preferredSize=[40,20];
win.g15.et1.enabled=false;
win.g15.bu1 = win.g15.add('button',undefined,'Add Action');
win.g15.bu2 = win.g15.add('button',undefined,'Remove Action');
win.p1.p3= win.p1.add("panel", undefined, undefined, {borderStyle:"black"}); 
win.p1.p3.preferredSize=[300,2];
win.g20 =win.p1.add('group');
win.g20.orientation = "row";
win.g20.alignment='center';
win.g20.spacing=10;
win.g20.bu1 = win.g20.add('button',undefined,'Process');
win.g20.bu1.preferredSize=[170,35];
win.g20.bu2 = win.g20.add('button',undefined,'Cancel');
win.g20.bu2.preferredSize=[170,35];

var actionSets = new Array();
actionSets = getActionSets();
for (var i=0,len=actionSets.length;i<len;i++) {
	win.g10.dd1.add ('item', "" + actionSets[i]);      
}; 
win.g10.dd1.selection=0;

var actions = new Array();	
actions = getActions(actionSets[0]);
for (var i=0,len=actions.length;i<len;i++) {
	win.g10.dd2.add ('item', "" + actions[i]);      
};
win.g10.dd2.selection=0;

win.g10.dd1.onChange = function() {
win.g10.dd2.removeAll();
actions = getActions(actionSets[parseInt(this.selection)]);
for (var i=0,len=actions.length;i<len;i++) {
	win.g10.dd2.add ('item', "" + actions[i]);  
	}
	win.g10.dd2.selection=0;
};
actionArray=[];
win.g15.bu1.onClick = function() {
	actionArray.push([[win.g10.dd2.selection.text],[win.g10.dd1.selection.text]]);
	win.g15.et1.text = actionArray.length;
}
win.g15.bu2.onClick = function() {
	actionArray.pop();
	win.g15.et1.text = actionArray.length;
}
win.g20.bu1.onClick=function(){
if(actionArray.length ==0) actionArray.push([[win.g10.dd2.selection.text],[win.g10.dd1.selection.text]]);
win.close(1);
if(win.g5.cb1.value){
    selectAllLayers();
    selectedLayers= getSelectedLayersIdx();
    }
if(win.g3.cb1.value) snapShot();
for(var a in selectedLayers){
    makeActiveByIndex(Number(selectedLayers[a]));
     if(app.activeDocument.activeLayer.kind != LayerKind.NORMAL) continue;
     for(var z in actionArray){
         try{
         doAction(actionArray[z][0].toString(), actionArray[z][1].toString());	
         }catch(e){alert(e+" - "+e.line);}
         }
    }

};
win.center();
win.show();

function getActionSets() { 
cTID = function(s) { return app.charIDToTypeID(s); }; 
sTID = function(s) { return app.stringIDToTypeID(s); }; 
  var i = 1; 
  var sets = [];  
  while (true) { 
    var ref = new ActionReference(); 
    ref.putIndex(cTID("ASet"), i); 
    var desc; 
    var lvl = $.level; 
    $.level = 0; 
    try { 
      desc = executeActionGet(ref); 
    } catch (e) { 
      break;   
    } finally { 
      $.level = lvl; 
    } 
    if (desc.hasKey(cTID("Nm  "))) { 
      var set = {}; 
      set.index = i; 
      set.name = desc.getString(cTID("Nm  ")); 
      set.toString = function() { return this.name; }; 
      set.count = desc.getInteger(cTID("NmbC")); 
      set.actions = []; 
      for (var j = 1; j <= set.count; j++) { 
        var ref = new ActionReference(); 
        ref.putIndex(cTID('Actn'), j); 
        ref.putIndex(cTID('ASet'), set.index); 
        var adesc = executeActionGet(ref); 
        var actName = adesc.getString(cTID('Nm  ')); 
        set.actions.push(actName); 
      } 
      sets.push(set); 
    } 
    i++; 
  } 
  return sets; 
}; 

function getActions(aset) {
cTID = function(s) { return app.charIDToTypeID(s); }; 
sTID = function(s) { return app.stringIDToTypeID(s); };
  var i = 1;
  var names = [];
  if (!aset) {
    throw "Action set must be specified";
  }  
  while (true) {
    var ref = new ActionReference();
    ref.putIndex(cTID("ASet"), i);
    var desc;
    try {
      desc = executeActionGet(ref);
    } catch (e) {
      break;
    }
    if (desc.hasKey(cTID("Nm  "))) {
      var name = desc.getString(cTID("Nm  "));
      if (name == aset) {
        var count = desc.getInteger(cTID("NmbC"));
        var names = [];
        for (var j = 1; j <= count; j++) {
          var ref = new ActionReference();
          ref.putIndex(cTID('Actn'), j);
          ref.putIndex(cTID('ASet'), i);
          var adesc = executeActionGet(ref);
          var actName = adesc.getString(cTID('Nm  '));
          names.push(actName);
        }
        break;
      }
    }
    i++;
  }
  return names;
};
function snapShot() {
    var desc9 = new ActionDescriptor();
        var ref5 = new ActionReference();
        ref5.putClass( charIDToTypeID('SnpS') );
    desc9.putReference( charIDToTypeID('null'), ref5 );
        var ref6 = new ActionReference();
        ref6.putProperty( charIDToTypeID('HstS'), charIDToTypeID('CrnH') );
    desc9.putReference( charIDToTypeID('From'), ref6 );
    desc9.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('HstS'), charIDToTypeID('FllD') );
    executeAction( charIDToTypeID('Mk  '), desc9, DialogModes.NO );
};
function hasLayerMask() { 
   var ref = new ActionReference(); 
   ref.putEnumerated( stringIDToTypeID( "layer" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" )); 
   var Mask= executeActionGet( ref ); 
   return Mask.hasKey(charIDToTypeID('Usrs')); 
};
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; 
 };
function makeActiveByIndex( idx, visible,add ){ 
    if(visible == undefined) visible = false;
    if(add == undefined) add=false;
    var desc = new ActionDescriptor(); 
      var ref = new ActionReference(); 
      ref.putIndex(charIDToTypeID( "Lyr " ), idx) 
      desc.putReference( charIDToTypeID( "null" ), ref ); 
      if(add) desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
      desc.putBoolean( charIDToTypeID( "MkVs" ), visible ); 
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
};
function selectAllLayers(layer) {
if(layer == undefined) layer = 0;
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
var BL = activeDocument.activeLayer.name;
activeDocument.activeLayer = activeDocument.layers[layer];
    var desc5 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putName( charIDToTypeID('Lyr '), BL);
    desc5.putReference( charIDToTypeID('null'), ref3 );
    desc5.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelectionContinuous') );
    desc5.putBoolean( charIDToTypeID('MkVs'), false );
    executeAction( charIDToTypeID('slct'), desc5, DialogModes.NO );
};
}

 

Translate
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