Skip to main content
Participant
April 14, 2016
Question

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

  • April 14, 2016
  • 3 replies
  • 5318 views

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?

This topic has been closed for replies.

3 replies

Disposition_Dev
Legend
April 20, 2016

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)

}

Participant
April 18, 2016

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

CarlosCanto
Community Expert
Community Expert
April 18, 2016

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

Silly-V
Legend
April 14, 2016

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.

Participant
April 15, 2016

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

Silly-V
Legend
April 15, 2016

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