Photoshop CC Mac looking for script: run action on all layers

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

bobtitus
Posts: 1
Joined: Mon Mar 13, 2023 10:59 pm

Photoshop CC Mac looking for script: run action on all layers

Post by bobtitus »

Hello,

I'm totally new to scripting, meaning I don't know how to script.
I'm looking for a script that would run action(s) on each layer of an opened file, one by one.
I have files with up to 100 layers and a an adjustment layer on top.
I need to get rid of this adjustment layer and apply the same settings to all layer below.
I've found this one (attached) that should have supposedly done the trick but it doesn't work on CC.
Could anybody help me?
Thank you!

Best,
Bob
Attachments
Run Action.jsx.zip
(48.96 KiB) Downloaded 128 times
User avatar
Stephen_A_Marsh
Posts: 29
Joined: Sun Aug 04, 2019 12:37 pm

Re: Photoshop CC Mac looking for script: run action on all layers

Post by Stephen_A_Marsh »

Bob, you saved the webpage HTML, not the JavaScript source code (use the raw button as below for the code):

https://raw.githubusercontent.com/Paul- ... Action.jsx

There are many other scripts to run an action on all or selected layers, here are some to try...

https://raw.githubusercontent.com/barba ... Layers.jsx

and

https://raw.githubusercontent.com/Paul- ... Action.jsx

and

Multiple selected layers (change the name of the action set and action in the code, exact spelling case sensitive):

Code: Select all

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/do-action-for-selected-layer/td-p/12604989
*/

app.bringToFront();
var selectedLayers= getSelectedLayersIdx();

var actionSets = new Array();
actionArray=[];

var Ai=("Action Name Here"); var As=("Action Set Here");
if(actionArray.length ==0) actionArray.push([[Ai],[As]]);

    selectedLayers= getSelectedLayersIdx();

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

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 ); 
};
Selected action in the action panel and selected layers:

Code: Select all

//Jazz-y
// Run currently selected action on selected layers.jsx

#target photoshop
var s2t = stringIDToTypeID;


(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p);

(r = new ActionReference()).putEnumerated(s2t('action'), s2t('ordinal'), s2t('targetEnum'));
try {
    try {
        var atn = executeActionGet(r).getString(s2t('name')),
            set = executeActionGet(r).getString(s2t('parentName'));
    }
    catch (e) { throw 'Before start select any action from actions palette!' }

    for (var i = 0; i < lrs.count; i++) {
        (r = new ActionReference()).putIdentifier(s2t('layer'), lrs.getReference(i).getIdentifier(s2t('layerID')));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        try { executeAction(s2t('select'), d, DialogModes.NO); } catch (e) { throw e + '\nCannot select layer!' }
        (r = new ActionReference()).putName(s2t('action'), atn);
        r.putName(s2t('actionSet'), set);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        try { executeAction(s2t('play'), d) } catch (e) { throw e + '\nCannot play action "' + atn + '" from set "' + set + '"' }
    }
} catch (e) { alert(e) }
Change the action set and action name in the code:

Code: Select all

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/applying-an-action-to-all-the-layers-in-a-document/td-p/12463132
Applying an action to all the layers in a document.
Courtesy of jazz-y
*/

#target photoshop

var s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p);

(r = new ActionReference());
r.putEnumerated(s2t('action'), s2t('ordinal'), s2t('targetEnum'));
var atn = executeActionGet(r).getInteger(s2t('itemIndex')),
    set = executeActionGet(r).getInteger(s2t('parentIndex'));

for (var i = 0; i < lrs.count; i++) {
    (r = new ActionReference()).putIdentifier(s2t('layer'), lrs.getReference(i).getIdentifier(s2t('layerID')));
    (d = new ActionDescriptor()).putReference(s2t('target'), r);
    executeAction(s2t('select'), d, DialogModes.NO);
    (r = new ActionReference()).putIndex(s2t('action'), atn); // Action name
    r.putIndex(s2t('actionSet'), set); // Action set name
    (d = new ActionDescriptor()).putReference(s2t('target'), r);
    try { executeAction(s2t('play'), d) } catch (e) { alert(e) }
}
and:

Code: Select all

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

Code: Select all

// 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 );  
}
And finally, this one isn't free, however it is inexpensive:

https://lowsunsamurai.com/store/MN6k/ap ... -photoshop