The answer you seek is in this command:
Here is the necessary ElementPlacement enumeration:
Some example code :
var doc = app.activeDocument; var myItem = doc.pathItems; myItem.move(doc.layers, ElementPlacement.INSIDE); // places item into another layer (if it starts in a different layer, obviously) //myItem.move(doc.layers, 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.
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.
You can also copy/cut the to-be-moved object and paste in front / paste in back of a selected object.
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
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
don't worry about it, we can help clarify things if you need more help.
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;
var objectToMove = doc.selection; // i don't know if this will work to determine the correct source and target objects
var targetPosition = targetObject.zOrderPosition;
while (objectToMove.zOrderPosition > targetPosition)