4 Replies Latest reply on Jul 8, 2010 10:06 AM by Michael L Hale

    Send backwards

    Ghoulfool Level 1

      Hi. How do you code it in javascript to send a layer backwards, as in Layer > arrange > send backward?  It#s part of a simple script that has two layers and I need to arrange them so the new layer moves from the top to underneath the other layer.


      I've got it as the following, but I'm after something cleaner


      // =======================================================
      var id122 = charIDToTypeID( "move" );
          var desc28 = new ActionDescriptor();
          var id123 = charIDToTypeID( "null" );
              var ref10 = new ActionReference();
              var id124 = charIDToTypeID( "Lyr " );
              var id125 = charIDToTypeID( "Ordn" );
              var id126 = charIDToTypeID( "Trgt" );
              ref10.putEnumerated( id124, id125, id126 );
          desc28.putReference( id123, ref10 );
          var id127 = charIDToTypeID( "T   " );
              var ref11 = new ActionReference();
              var id128 = charIDToTypeID( "Lyr " );
              var id129 = charIDToTypeID( "Ordn" );
              var id130 = charIDToTypeID( "Prvs" );
              ref11.putEnumerated( id128, id129, id130 );
          desc28.putReference( id127, ref11 );
      executeAction( id122, desc28, DialogModes.NO );



        • 1. Re: Send backwards
          Paul Riggott Level 6

          You could create a function IE:

          moveLayerUpOrDown("Down"); // "UP" or "DOWN"
          function moveLayerUpOrDown(Direction) {
              case 'up' : Direction = 'Nxt '; break;
              case 'down' : Direction = 'Prvs'; break;
              default : Direction = 'Prvs'; break;
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putEnumerated(charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
          desc.putReference(charIDToTypeID('null'), ref );
          var ref2 = new ActionReference();
          ref2.putEnumerated(charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID(Direction) );
          desc.putReference(charIDToTypeID('T   '), ref2 ); executeAction(charIDToTypeID('move'), desc, DialogModes.NO );
          • 2. Re: Send backwards
            Michael L Hale Level 5

            You could also add"Back" and "Frnt" for 'send to back' and 'send to front'.


            About the best you can do with scriptlistner log output is clean it up and turn it into a function like in Paul's post. It may never be as 'clean' as a DOM method but it is often the only way to do something. Or it is either faster or eaiser than the DOM.


            You can move layers using the DOM but you need to create layer references and take layerSets into account. The advantage of using the DOM is if you know the layer structure you can move the layer anywhere in the layer stack.



            var lyr1 = app.activeDocument.artLayers[1];
            var lyr2 = app.activeDocument.artLayers[2];
            lyr1.move(lyr2, ElementPlacement.PLACEAFTER);// same as menu item send backward in this case
            1 person found this helpful
            • 3. Re: Send backwards
              Ghoulfool Level 1



              Thanks. the DOM version makes more sense to me. But I'm still a bit confused as to why, in order to get it to work I had to change the art layers to 0 and 1. Which now works (with or without an extra background layer in) Is that anything to do with layersets?? - (which I've not covered yet BTW)


              var lyr1 = app.activeDocument.artLayers[0];
              var lyr2 = app.activeDocument.artLayers[1];
              lyr1.move(lyr2, ElementPlacement.PLACEAFTER);

              • 4. Re: Send backwards
                Michael L Hale Level 5

                That was just an example and would only work if the doc has three layers that are not the background. You can't move the background layer or move a layer below it. That is why you had to change your indexes. You can make the layer references any way you like. For example you could use getByName. You could also use layers instead of artLayers, etc.


                If you are trying to make a scripting version of the 'send backward' and the document has layerSets you may need to use INSIDE, PLACEATBEGINNING,

                or PLACEATEND for the ElementPlacement if the relativeObject is a layerSet.