Copy link to clipboard
Copied
I am looking to find a way to change the order objects are layered in the layers panel. Ideally, Perhaps a script that runs down the page, reordering objects (in the layers panel) based on the y and then x value. This way, a logical order is created in the Layers panel.
There has been a similar querie for Illustrator found here:
https://community.adobe.com/t5/illustrator/auto-arrange-per-y-value-position/td-p/10024991?page=1
Can anyone help?
A little tricky since items on a given layer can be spread across several pages. So you have to go page by page to get the items on a given layer, sort them and rearrange. Something like this could work, but perhaps someone has a more elegant solution. Also won't hit pasteboard items. Use at your own risk 😉
var main = function() {
var pages = app.documents[0].spreads.everyItem().getElements();
var layerName = "myLayerName";
var layerArr,
apis;
for (var i = 0; i < p
...
Hi Brian,
the logical point to start with this is not the page, I think, but the spread. From the perspective of the GUI the layers panel will show all items on the active spread and not the items of a page on that spread.
And you do not do that with the allPageItems array, but with the pageItems collection of a spread, because you cannot assign a new value for itemLayer if an object is nested in other objects like allPageItems will show them.
Regards,
Uwe Laubender
( ACP )
Thanks, Uwe. Some good points. Doing spread would allow us to re-sort items on the pasteboard, and we probably only need to move top-level page items, but I am not reassigning itemLayer; I am just reordering the objects (.sendToBack()) since layer ordering is predicated on the object's front/back position. It could be done by either Page or Spread, since the end result would be the same as far as the GUI is concerned, but as noted, doing spread captures PB items.
I've made some adjustments to
...Change "myLayerName" on the second line of code to whatever the layer name is. Alternartively, if there is only one layer, you can change that to: "Layer 1"
Copy link to clipboard
Copied
A little tricky since items on a given layer can be spread across several pages. So you have to go page by page to get the items on a given layer, sort them and rearrange. Something like this could work, but perhaps someone has a more elegant solution. Also won't hit pasteboard items. Use at your own risk 😉
var main = function() {
var pages = app.documents[0].spreads.everyItem().getElements();
var layerName = "myLayerName";
var layerArr,
apis;
for (var i = 0; i < pages.length; i++) {
layerArr = new Array();
apis = pages[i].pageItems;
for (var j = 0; j < apis.length; j++) {
if (apis[j].itemLayer.name == layerName) {
layerArr.push(apis[j]);
}
}
sortByYX(layerArr);
for (var k = 0; k < layerArr.length; k++) {
try { layerArr[k].sendToBack(); } catch(e) {}
}
}
}
var sortByYX = function(arr) {
arr.sort(function (a, b) {
if (a.geometricBounds[0] < b.geometricBounds[0]) {
return -1;
} else if (a.geometricBounds[0] > b.geometricBounds[0]) {
return 1;
} else {
return a.geometricBounds[1] > b.geometricBounds[1];
}
});
};
main();
Copy link to clipboard
Copied
Hi Brian,
the logical point to start with this is not the page, I think, but the spread. From the perspective of the GUI the layers panel will show all items on the active spread and not the items of a page on that spread.
And you do not do that with the allPageItems array, but with the pageItems collection of a spread, because you cannot assign a new value for itemLayer if an object is nested in other objects like allPageItems will show them.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thanks, Uwe. Some good points. Doing spread would allow us to re-sort items on the pasteboard, and we probably only need to move top-level page items, but I am not reassigning itemLayer; I am just reordering the objects (.sendToBack()) since layer ordering is predicated on the object's front/back position. It could be done by either Page or Spread, since the end result would be the same as far as the GUI is concerned, but as noted, doing spread captures PB items.
I've made some adjustments to my script.
Copy link to clipboard
Copied
Thank you very much for taking a look and providing the code. Unfortunately, the script doesn't make any changes to the layer order.
Copy link to clipboard
Copied
Change "myLayerName" on the second line of code to whatever the layer name is. Alternartively, if there is only one layer, you can change that to: "Layer 1"
Copy link to clipboard
Copied
Pefect! Thank you so much. Have a great day 🙂