Copy link to clipboard
Copied
Hello all,
Is there a way in Indesign where we can traverse the items on page in z-order (like in Illustrator) from top to bottom or bottom to top via script?
Hi @Developer1009 , would this work for you?:
var lyrs = app.activeDocument.layers
//make the first spread active
app.activeWindow.activeSpread = app.activeDocument.spreads[0];
var pi;
for (var i = 0; i < lyrs.length; i++){
pi = lyrs[i].pageItems
for (var j = 0; j < pi.length; j++){
$.writeln(pi[j].index + " " + pi[j].name)
};
};
Copy link to clipboard
Copied
Hi Developer1009,
per spread you can use the array allPageItems.
For a given page test for parentPage and also exclude all elements where the value for parent is not the spread, elements in nested structures like groups or anchored objects in text for example.
Array arrayOfItemsInZOrderOnPage lists all items on page 1 of the active document in z-order ( nested items are ignored ) :
var myPage = app.documents[0].pages[0];
var mySpread = myPage.parent;
var allItemsOnSpread = mySpread.allPageItems;
var arrayOfItemsInZOrderOnPage = [];
for( var n=0; n<allItemsOnSpread.length; n++ )
{
if( allItemsOnSpread[n].parentPage != myPage ){ continue };
if( allItemsOnSpread[n].parent != mySpread ){ continue };
arrayOfItemsInZOrderOnPage[ arrayOfItemsInZOrderOnPage.length++ ] =
allItemsOnSpread[n];
};
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thank you @Laubender
Is it not possible with the property of pageItems instead of allPageItems?
Copy link to clipboard
Copied
Reliably? Without further ado? Don't think so…
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
You'd have to write a sort algorithm on the page items based on their geometric bounds. Rudimentarily, one could compare sort by the [0] point:
var apis = page.allPageItems;
apis.sort(function(a,b) {
return a.geometricBounds[0] < b.geometricBounds[0];
})
You might want other sort logic though?
Copy link to clipboard
Copied
Thank uou @brianp311
But I am not trying to acheive this based on geometric bounds, it is based on the how page elements visually display in Layers panel..
Copy link to clipboard
Copied
Hi @Developer1009 , would this work for you?:
var lyrs = app.activeDocument.layers
//make the first spread active
app.activeWindow.activeSpread = app.activeDocument.spreads[0];
var pi;
for (var i = 0; i < lyrs.length; i++){
pi = lyrs[i].pageItems
for (var j = 0; j < pi.length; j++){
$.writeln(pi[j].index + " " + pi[j].name)
};
};