8 Replies Latest reply on May 31, 2018 6:06 PM by Stephen_A_Marsh

    Apply an action to all selected layers

    showshow Level 1



      I wish to do an action i 've made (select a specific zone and deleted it), on every layer i selected.

      It can be 2 to 28 layers...


      I hope you can help me


        • 2. Re: Apply an action to all selected layers
          showshow Level 1

          It's sublime,

          • 3. Re: Apply an action to all selected layers
            showshow Level 1

            Hi WonderMerlin

            OK, i wish to scale all my layers to a selection, do you have an issue for this?

            • 5. Re: Apply an action to all selected layers
              showshow Level 1
              #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('Scale layer to select', 'SHOES MODIF TAILLE');// ici defi l'action pour les layers selectiones
              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 );
                  executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
              ////// 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++){
                             selectedLayers.push(  desc.getReference( i ).getIndex() );
                             selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
                       var ref = new ActionReference();
                       ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
                       ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
                          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
                    return selectedLayers;
              2 people found this helpful
              • 6. Re: Apply an action to all selected layers
                X-Raym Level 1

                I'm very surprised there is still no native concized way to check if a layer is selected or not, seems pretty essential function for a majority of layer related scripts.


                It should be a layer property IMHO, or at least there should be a IsLayerSelected(layer) function (or alternative getter/setter).

                • 7. Re: Apply an action to all selected layers

                  your script is amazing, thank you so much!!

                  • 8. Re: Apply an action to all selected layers
                    Stephen_A_Marsh Adobe Community Professional

                    Thank you for sharing!


                    (For those new to scripting, It is all about editing line 13 to reference the loaded action name and action set name.


                    A similar recent topic thread here:


                    Is there a way to resize the height of multiple images at once without setting a specific width?