6 Replies Latest reply on Jun 12, 2018 12:25 PM by MBA-2-rae

    How to set several artLayers as activeLayer at the same time?

    Curvin

      Now I know several artLayers,and I want to set them as activeLayer at the same time. But one artLayer can be set as activeLayer when I use app.activeDocument.activeLayer

       

      help~help~help~ 

        • 1. Re: How to set several artLayers as activeLayer at the same time?
          SuperMerlin Level 5

          activeLayer means ONE layer only!

          You can have several layers selected  but activeDocument.activeLayer will return details of the top most selected layer.

          • 2. Re: How to set several artLayers as activeLayer at the same time?
            Curvin Level 1

            SuperMerlin, Thank you for your reply!

            So we can not select several layers with code? Now I want to select several layers and merge them with code.

            • 3. Re: How to set several artLayers as activeLayer at the same time?
              Curvin Level 1

              As we all know, we can select several layers in the PS panel, but can we select several layers with code?

              And now I have gotten several layers .

              • 4. Re: How to set several artLayers as activeLayer at the same time?
                SuperMerlin Level 5

                You can select layers by Index , Id or by Name

                Or select all layers.

                Here are some functions that will let you do all the options including the merge.

                 

                function selectLayerByIndex(index,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 selectLayerById(id,add){ 
                var ref = new ActionReference();
                ref.putIdentifier(charIDToTypeID("Lyr "), id);
                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 selectLayerByName(lyrName,add){ 
                add = undefined ? add = false:add 
                var desc = new ActionDescriptor(); 
                var ref = new ActionReference(); 
                ref.putName( charIDToTypeID( "Lyr " ), lyrName); 
                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 ); 
                };
                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 mergeSelected() {
                try{
                executeAction( charIDToTypeID('Mrg2'), undefined, DialogModes.NO );
                }catch(e){}
                };
                
                
                
                • 6. Re: How to set several artLayers as activeLayer at the same time?
                  MBA-2-rae Level 1

                  To select several layers with code I use this function, where the layer-IDs to be selected are stored in an array.

                   

                  // layers to be selected (convert layer index to id:

                  id1 = getLayerId(activeDocument.layers[0])  //

                  id2 = getLayerId(activeDocument.layers[2])

                  // add more IDs if wanted

                   

                  // store the layer-IDs into an array:

                  ary = [id1, id2]

                   

                  // call the function

                  ret = selectLyr()

                   

                   

                  function selectLyr(lyrAry) {

                      ////////////////////////////////////////////////////////

                      // Description: Selects multiple layers

                      // Parameters: lyrAry (Array):  Contains the layer-IDs to be  selected

                      //  Return: boolean

                      ////////////////////////////////////////////////////////

                     

                      if (lyrAry==undefined || lyrAry.constructor != Array )  return  false

                   

                      var d = new ActionDescriptor();

                      var r = new ActionReference();

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

                          r.putIdentifier( s2t( "layer" ), lyrAry[i] ) ;                         // Array of layer by ID

                      }

                                                                             

                      d.putReference( c2t( "null" ), r );

                      d.putEnumerated( s2t( "selectionModifier" ), s2t( "selectionModifierType" ), s2t(  "addToSelection") );

                      d.putBoolean( s2t( "makeVisible" ), true );

                      try {executeAction( s2t( "select" ), d, DialogModes.NO ); return true;}

                      catch(e) { return false }

                  }

                   

                  function getLayerId(theLayer){

                      // by mike hale, via paul riggott;

                      // http://forums.adobe.com/message/1944754#1944754

                      app.activeDocument.activeLayer = theLayer;      // theLayer must be object

                      //Assumes activeDocument and activeLayer  

                      var r = new ActionReference();

                      r.putEnumerated(c2t("Lyr "), c2t("Ordn"), c2t("Trgt"));

                      d = executeActionGet(r);

                      return d.getInteger( c2t('LyrI') );

                  };

                   

                  function c2t (s) {return app.charIDToTypeID(s)};

                  function s2t (s) {return app.stringIDToTypeID(s)};