4 Replies Latest reply on Feb 17, 2014 7:13 AM by Pedro Cortez Marques

    Move a layer/layerset into a layerset using ID only

    oddgames Level 1

      I'm trying to move a layer into a layerset using it's ID (not name or index).

       

      This function below works if I use putName instead of putIdentifier, does anyone have some code that will move a layer into a layerset using only ID?

       

      I keep getting General photoshop error occurred.

       

           moveLayer : function( fromLayer, toLayer )

           {

                        

              var desc5 = new ActionDescriptor();

              var ref4 = new ActionReference();

              ref4.putIdentifier( charIDToTypeID('Lyr '), Number(fromLayer) );

              desc5.putReference( charIDToTypeID('null'), ref4 );

              var ref5 = new ActionReference();

              ref5.putIndex( charIDToTypeID('Lyr '), this.getLayerIndex(toLayer) );

              desc5.putReference( charIDToTypeID('T   '), ref5 );

              desc5.putBoolean( charIDToTypeID('Adjs'), false );

              desc5.putInteger( charIDToTypeID('Vrsn'), 5 );

             

              try

              {

              executeAction( charIDToTypeID('move'), desc5, DialogModes.NO );

              }

              catch(e)

              {

                  alert(e);

              }

       

       

             

          },

       

       

       

          // Get layer index but return 0 if there is an error.

          getLayerIndex: function(id)

          {

       

       

              var ref = new ActionReference();

              ref.putIdentifier( charIDToTypeID('Lyr '), id );

             

              try

              {

                  activeDocument.backgroundLayer;

                  return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;

                 

              }

              catch(e)

              {

                  try

                  {

                      return  executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));

                  }

                   catch(e)

                   {

                       return 0;

                   }

              }

       

       

          },

        • 1. Re: Move a layer/layerset into a layerset using ID only
          Pedro Cortez Marques Level 3

          It is simpler using this?

           

          var thisLayer = activeDocument.activeLayer;

          // create new layerset if doesn't exist

          if (activeDocument.layerSets.length == 0) activeDocument.layerSets.add();

          // return to previous selected layer

          activeDocument.activeLayer = thisLayer;

          // move layer to the referenced layerSets[0] but the id of this element could be any number

          activeDocument.activeLayer.move (activeDocument.layerSets[0], ElementPlacement.PLACEATEND);

           

          So the solution is to create first a script for(var a in activeDocument.layerSets) to reference your chosen layerSet by id and if exists it gives you the index.

          • 2. Re: Move a layer/layerset into a layerset using ID only
            oddgames Level 1

            My extension has it's own "layer list" that represents only a hand ful of layers that have meta data that has been set by the extension.

             

             

            I need to be able to move exisiting layers into existing layer sets using their ID.

             

            Does this line of code place it within the layerset? Or after it?

            activeDocument.activeLayer.move (activeDocument.layerSets[0], ElementPlacement.PLACEATEND);

            • 3. Re: Move a layer/layerset into a layerset using ID only
              oddgames Level 1

              This seems to work:

               

               

               

               

                   moveInside : function( fromLayerID, toLayerID )

                   {

               

               

                      var desc = new ActionDescriptor();

                      var sourceRef = new ActionReference();

                      sourceRef.putIdentifier( charIDToTypeID( "Lyr " ), fromLayerID );

                      desc.putReference( charIDToTypeID( "null" ), sourceRef );

                      var layerIndex = this.getLayerIndex(toLayerID);

                      var destinationRef = new ActionReference();

                      destinationRef.putIndex( charIDToTypeID( "Lyr " ), layerIndex-1 );

                      desc.putReference( charIDToTypeID( "T   " ), destinationRef );

                      desc.putBoolean( charIDToTypeID( "Adjs" ), false );

                      desc.putInteger( charIDToTypeID( "Vrsn" ), 5 );

                      executeAction( charIDToTypeID( "move" ), desc, DialogModes.NO );

               

               

                  },

               

               

                  // Get layer index but return 0 if there is an error.

                  getLayerIndex: function(id)

                  {

               

               

                      var ref = new ActionReference();

                      ref.putIdentifier( charIDToTypeID('Lyr '), id );

                     

                      try

                      {

                          activeDocument.backgroundLayer;

                          return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;

                         

                      }

                      catch(e)

                      {

                          try

                          {

                              return  executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));

                          }

                           catch(e)

                           {

                               return 0;

                           }

                      }

               

               

                  },

              • 4. Re: Move a layer/layerset into a layerset using ID only
                Pedro Cortez Marques Level 3

                move(1st argument , 2nd argument)

                 

                1st argument:

                the layerSet or another layer

                 

                2nd argument:

                once layerSet has been set, this will set the layer in 1 of these 3 options:

                // there are 3 types of placement:

                ElementPlacement.PLACEATEND

                ElementPlacement.PLACEAFTER // use this when using move() to place a layer after another layer

                ElementPlacement.PLACEBEFORE // use this when using move() to place a layer before another layer

                 

                If you want to move layers inside a layerSet the logic os the same but using only layers.