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

Script I made: Iterate Action Over Layers

Participant ,
Jul 04, 2016 Jul 04, 2016

Copy link to clipboard

Copied

Hey, just cobbled together this script that I'm already finding useful in my own work, so I figured I'd share, and maybe see if anyone has suggestions for improvement.

What it does, is apply a selected action to each of the selected layers individually.

So for example, if you had 50 layers that you wanted to each rotate by 90 degrees in place, you'd record a rotate-by-90-degrees action, select all the layers you want to apply it to and run the script with the action selected.

#target photoshop

var scriptName = "NinjaScript IterateActionLayers";

var scriptVersion = "0002";

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 = 100;

   

  OpenOptionsDialog = new Window("dialog", scriptName + " v" + scriptVersion);

  OpenOptionsDialog.orientation = 'column';

  OpenOptionsDialog.alignChildren = 'left';

    mainGroup = OpenOptionsDialog.add("group");

    mainGroup.orientation = 'column';

    mainGroup.alignChildren = 'left';

    mainGroup.alignment = 'left';    

    var actionSetGroup = mainGroup.add("group");

    actionSetGroup.orientation = 'row';

    actionSetGroup.add("statictext",undefined, "ActionSet: ")

    var DDActionSet = actionSetGroup.add("dropdownlist",undefined, "")

    DDActionSet.preferredSize.width = 150;

      

    for (var i = 0; i < currentActionSets.length; i++)

    {

            DDActionSet.add("item", currentActionSets);

    }

    DDActionSet.selection = 0;

      

      

    var actionGroup = mainGroup.add("group");

    actionGroup.orientation = 'row';

    actionGroup.add("statictext",undefined, "Action:      ")

    DDActions = actionGroup.add("dropdownlist",undefined, "")

    DDActions.preferredSize.width = 150;

   

    function populateDDActions (inSet)

    {

        DDActions.removeAll();

       

        for (var i = 0; i < currentActionSets[inSet].actions.length; i++)

        {

            DDActions.add("item", currentActionSets[inSet].actions);

        }

        DDActions.selection = 0;

    }

    DDActionSet.onChange = function()

    {

        populateDDActions(DDActionSet.selection.index);

    }

    DDActionSet.onChange();

   

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

   {

       var SelectedLayers = getSelectedLayers();

      

        for (var i = 0; i < SelectedLayers.length; i++)

        {

            activeDocument.activeLayer = SelectedLayers;

            doAction(DDActions.selection.text, DDActionSet.selection.text);

           

        }

        OpenOptionsDialog.close()

    }

   

    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 getSelectedLayers()

{

  var resultLayers=new Array();

  try{

    var descGrp = new ActionDescriptor();

    var refGrp = new ActionReference();

    refGrp.putEnumerated(cID( "Lyr " ),cID( "Ordn" ),cID( "Trgt" ));

    descGrp.putReference(cID( "null" ), refGrp );

    executeAction( sID( "groupLayersEvent" ), descGrp, DialogModes.NO );

    for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}

    var desc5 = new ActionDescriptor();

    var ref2 = new ActionReference();

    ref2.putEnumerated( cID( "HstS" ), cID( "Ordn" ), cID( "Prvs" ) );

    desc5.putReference( cID( "null" ), ref2 );

    executeAction( cID( "slct" ), desc5, DialogModes.NO );

  } catch (err) { }

  return resultLayers;

}  

TOPICS
Actions and scripting

Views

8.0K

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 Expert ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

So you don't want a GUI? You just want to select the layers and run the script and have a preset hard-coded action run?

Votes

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
Participant ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

Correct. Although I would rather use an easier script that do the task faster, this the only one I found that works.

Votes

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
LEGEND ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

LATEST

function layerUpdate(){
if(documents.length > 0){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
try{
var docRef = activeDocument;
for(var i = 0; i < docRef.artLayers.length; i++){
var LayerRef = docRef.artLayers[i];
docRef.activeLayer = LayerRef;
doAction(test, TEST);
}
app.displayDialogs = originalDialogMode;
}
catch(e){
}
}
}

Votes

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
Community Beginner ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

Hmmm, i would say no. The script just won't know what Action you want to apply.

The only way i see is to hard-code ActionSet and Action names in it, if you really need to.

Votes

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
Participant ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

Ok, Stephen_A_Marsh posted several scripts in this thread that Runs actions on layers without Dialog for anyone interested: Apply Layer Mask to Numerous Layers (without grouping)

 

 

Votes

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