7 Replies Latest reply on Apr 20, 2016 11:59 AM by williamadowling

    Send to back/front of other selected object by script?

    Pi Creative Studio

      Is there a way to do a script to send to back/front of other selected object, or to target the command send to back/front in relation to another object in the file? Even in different layers?

        • 1. Re: Send to back/front of other selected object by script?
          Silly-V Adobe Community Professional

          The answer you seek is in this command:

           

          PageItem move (relativeObject: Object, insertionLocation: ElementPlacement)


          Here is the necessary ElementPlacement  enumeration:

          INSIDE

          PLACEATBEGINNING

          PLACEATEND

          PLACEBEFORE

          PLACEAFTER



          Some example code :


          var doc = app.activeDocument;
          var myItem = doc.pathItems[0];
          myItem.move(doc.layers[1], ElementPlacement.INSIDE); // places item into another layer (if it starts in a different layer, obviously)
          //myItem.move(doc.layers[1], ElementPlacement.PLACEAFTER); // throws error "Cannot move to specified location"
          
          myItem = doc.pathItems.getByName('test');
          myItem.move(doc.groupItems.getByName('my group'), ElementPlacement.PLACEBEFORE); // moves it above a group named 'my group' in the stacking order
          myItem.move(doc.groupItems.getByName('my group'), ElementPlacement.PLACEAFTER); // moves it below a group named 'my group' in the stacking order
          myItem.move(doc.groupItems.getByName('my group'), ElementPlacement.INSIDE); // moves it inside a group named 'my group', at the top of the stacking order
          myItem.move(doc.groupItems.getByName('my group'), ElementPlacement.PLACEATBEGINNING); // moves it inside a group named 'my group', at the top of the stacking order just like ElementPlacement.INSIDE
          myItem.move(doc.groupItems.getByName('my group'), ElementPlacement.PLACEATEND); // moves it inside a group named 'my group', at the bottom of the stacking order
          
          

           

          Now let's see if it can even work with different documents!

           

            var doc = app.activeDocument;
            var myItem = doc.pathItems.getByName('test');
          
            var newDoc = app.documents.add();
            var newLayer = newDoc.layers.add();
            newLayer.name = "New Layer!";
            myItem.move(newLayer, ElementPlacement.INSIDE); // moves the item into a specified new layer in a new script-added document, although my AI crashed when doing this test for the 1st time. Not sure why, but it's good now. 
          
          
          • 2. Re: Send to back/front of other selected object by script?
            Pi Creative Studio Level 1

            I wish I had the knowledge to understand this code and use it in a script "tailored" by me!   Thanks Silly-V for the help. I believe the problem I have purposed is a common necessity from other people and sooner or later there will be a native command  in illustrator that will allow you to position an object above or below another one. Maybe even a plugin.

             

            If anyone already know somtething that already do something similar, please share.

             

            Thanks

            • 3. Re: Send to back/front of other selected object by script?
              Silly-V Adobe Community Professional

              You can also copy/cut the to-be-moved object and paste in front / paste in back of a selected object.

              • 4. Re: Send to back/front of other selected object by script?
                CarlosCanto Adobe Community Professional & MVP

                what do you need help with understanding the code? are you trying to add Silly-V into another script?

                 

                if you're just trying to script moving objects in front/back of others objects, you can do that with the UI, use Ctrl+F and Ctrl+B

                • 5. Re: Send to back/front of other selected object by script?
                  Pi Creative Studio Level 1

                  Not at all Carlos. I am very sorry if I did not make myself clear. I really appreciate the help given by Silly-V and you.  I will learn with the answers and try to build this script or use your sugestions. Thank you

                  • 6. Re: Send to back/front of other selected object by script?
                    CarlosCanto Adobe Community Professional & MVP

                    don't worry about it, we can help clarify things if you need more help.

                    • 7. Re: Send to back/front of other selected object by script?
                      williamadowling Level 4

                      Perhaps you could log the zOrderPosition of the targetObject, then move the desired object down in the list until it's zOrderPosition is less than the target's zOrder Position? Something like below.. (this is untested).

                       

                      var targetObject = doc.selection[0];

                      var objectToMove = doc.selection[1]; // i don't know if this will work to determine the correct source and target objects

                      var targetPosition = targetObject.zOrderPosition;

                       

                      while (objectToMove.zOrderPosition > targetPosition)

                      {

                           objectToMove.zOrder(ZOrderMethod.SENDBACKWARD)

                      }