Copy link to clipboard
Copied
Hello,
How to rearrange selected objects stacking order by its postion left to right, by any trick or script?
the top-left object should be at top level in stacking order. and this trick should be work on any type of object Raster/Vector/Group/Text item
Copy link to clipboard
Copied
J,
I am sure there is a script for that, but until someone presents/makes it, you can apply a simple Move to Back action, if that is manageable (you have a limited number). The action could just consist of Cut and Paste in Back (Ctrl/Cmd+X+B), to be applied to each object (set) from right to left, in other words in reverse order.
Such an action can also be used, and even called for under more woolly circumstances where a simple script would fail, such as multiple ungrouped objects on top of one another to be moved together without changing their internal order, including objects on top of one another to be kept together in the stacking order even with one/some being to the right of an unrelated obejct.
With a script it could be nice, or needed, to have a choice of reference point, left/centre/right.
Copy link to clipboard
Copied
Thanks, but I am looking very quick solution, due to there may be huge items in furure.
Copy link to clipboard
Copied
I quite understand, J.
I can see that you have set it as a Scripting topic, so I wonder why no scripter friend has answered.
Any scripter?
Copy link to clipboard
Copied
Make a new layer. Make a rectangle in this layer. Duplicate the layer as many times as you need. Place the image layer at the bottom. Select the top image and the rectangle (this will select the top rectangle). Group/Ungroup. The image is now on the top layer. Hide this layer and do the same with the next image and the next layer. You use that when grouping, everything goes to the layer of that object, which is highest in the stack.
Copy link to clipboard
Copied
var sel = app.activeDocument.selection;
var items = [];
for (var i = 0; i < sel.length; i++) {
var item = sel[i];
items.push({item: item, x: item.position[0]});
}
items.sort(function(a, b) { return a.x < b.x; }); // or use `>` to reverse order
for (var i = 0; i < items.length; i++) {
items[i].item.zOrder(ZOrderMethod.BRINGTOFRONT);
}
Copy link to clipboard
Copied
@hhas01 thank you the reply.
top-left image/object should be stay at top level at stacking order. accordingly other object's all the stacking order should be update. which is not happening by current code.
Copy link to clipboard
Copied
The previous script moved the leftmost selected object to the top of the stacking order.
This keeps the leftmost selected object at its current position in the stacking order:
var sel = app.activeDocument.selection;
if (sel.length < 2) {
alert("Please select 2 or more items.");
} else {
var items = [];
for (var i = 0; i < sel.length; i++) {
var item = sel[i];
items.push({item: item, x: item.position[0]});
}
items.sort(function(a, b) { return a.x < b.x; });
var topItem = items.pop().item;
while (items.length > 0) {
items.shift().item.move(topItem, ElementPlacement.PLACEAFTER);
}
}