Copy link to clipboard
Copied
Copy link to clipboard
Copied
Cheers @samia84304672 ...do you remember this solution? The page of the link is gone... 😕
I am looking for an answer to this too
Copy link to clipboard
Copied
Hi @Johan5C42 ,
the allPageItems array reflects the stacking order of elements.
Spread by spread, if the parent of a page item is the spread.
Regards,
Uwe Laubender
( Adobe Community Professional )
Copy link to clipboard
Copied
Perhaps I should go more into details with the allPageItems array of a specific spread.
In code, select the top most item on the spread:
var doc = app.documents[0];
var firstSpread = doc.spreads[0];
var firstSpreadAllPageItems = firstSpread.allPageItems;
var stackingOrderOfItems = [];
for( var n=0; n<firstSpreadAllPageItems.length; n++ )
{
// THIS WAS THE WRONG APPROACH SO IT FAILED TO EXCLUDE THE NESTED ITEMS:
// if(!firstSpreadAllPageItems[n].parent == firstSpread ){ continue };
// WHAT IS WORKING (Corrected after tested Johan's sample document below):
if( firstSpreadAllPageItems[n].parent.constructor.name != "Spread" ){ continue };
stackingOrderOfItems[stackingOrderOfItems.length++] =
firstSpreadAllPageItems[n];
};
// Select the top most item on the spread:
stackingOrderOfItems[0].select();
Or the one that is at the bottom:
stackingOrderOfItems[stackingOrderOfItems.length-1].select();
Regards,
Uwe Laubender
( Adobe Community Professional )
NOTE: Message and code edited after @Johan5C42 tested the code not working as expected.
Copy link to clipboard
Copied
Thank you Uwe! What a great community this is!
I did a test of your script, and it indeed finds the top and bottom items but if you take a look at all the listed items the order is incorrect.
I have made a brute force script that lists the correct order (reversed from your script, but that can be fixed 😉 )
My test file is attached, with it I get the following results:
Result allPageItems script:
[object Rectangle],[object Group],[object TextFrame],[object Oval],[object Polygon],[object TextFrame],[object Oval],[object TextFrame]
Result Brute force script:
[object TextFrame],[object Oval],[object TextFrame],[object Polygon],[object Group],[object Rectangle]
Regards
Johan
Brute force method:
var myDoc = app.documents[0];
var myPage = myDoc.pages[0];
var arrayToGroup =[];
var myPageItems = myDoc.pageItems.everyItem().getElements();
var decimatedPageItems = myPageItems;
zOrderedItems = [];
for(var mpi=myPageItems.length-1;mpi>=0;mpi--){
findObjectInTheVeryBack(decimatedPageItems,zOrderedItems);
}
function findObjectInTheVeryBack(pageItems,zOrderedItems){
//Brute force method
for (var i = 0 ; i < pageItems.length; i++) {
var theItem = pageItems[i];
theItem.select();
var menuActionBack = app.menuActions.item("$ID/Send to Back");
var notInTheVeryBack = menuActionBack.enabled;
if ( notInTheVeryBack == false){
//remove object from original array and return, add to new array, send to front :O
zOrderedItems.push(theItem);
var menuActionFront = app.menuActions.item("$ID/Bring to Front");
if(menuActionFront.enabled) menuActionFront.invoke();
pageItems.splice(i,1);
return(pageItems,zOrderedItems);
}
}
};
Copy link to clipboard
Copied
Ahh I see now what is happening, allItems lists both the Group and the items of the group (which has a different stacking order than the group) that is a bit tricky... but it would be possible to remove items which has a group as parent in the script, I will give it a try 🙂
Copy link to clipboard
Copied
Hi @Johan5C42 ,
sorry, had a bug in my code.
The result array gathered all elements, not only the ones where the parent is the spread.
E.g. it gathered also the ones inside the group of your test document.
In my code, instead:
if(!firstSpreadAllPageItems[n].parent == firstSpread ){ continue };
do:
if( firstSpreadAllPageItems[n].parent.constructor.name != "Spread" ){ continue };
Then my code is working on your sample document.
And I think it does not make sense to gather the allPageItems array of the document.
Instead do this spread by spread.
Note: Will correct my code above and comment about the correction.
Regards,
Uwe Laubender
( Adobe Community Professional )
Copy link to clipboard
Copied
YES! I did another solution, checking the parent in the loop, but yours is, of course much more elegant!
And agree, it should be allPageItems per spread only, sadly, I wish the stacking was done for the whole document ( I need to move items between pages and stack them correctly) but I have seen that InDesign only has this information per spread ( also when you drag an item manually to another spread and back to the original spread it looses it's original stacking order and gets placed on top) I will have to solve this in some other way.
Anyhow, this is a great help to me! Big thanks Uwe!
Regards
Johan
Copy link to clipboard
Copied
To all seeking answers in this thread:
This thread started in January 2009. At that time InDesign CS4 version 6.0 was just out ( October 2008 ).
The answer for Page Item Stacking Order back then was different than it is now.
All that changed with InDesign CS5 version 7.0 released in May 2010.
Regards,
Uwe Laubender
( Adobe Community Professional )
Copy link to clipboard
Copied
It seems that even the array "allPageItems" is built in the correct z-order, beginning from the top to the bottom, regardless of the object type - just as if the objects are put together as they appear in the Layers panel.
However, this is just an empiric finding -- so: Could someone of the Adobe folks please confirm this finding as the actual InDesign behaviour or tell us that we just have been lucky so far? Thanks a lot!
Copy link to clipboard
Copied
Rudi said: "Could someone of the Adobe folks please confirm this finding as the actual InDesign behaviour or tell us that we just have been lucky so far?"
Hi Rudi,
I would ask that at InDesign Prerelease.
Doubt that you'll get a reaction from InDesign engineers here in the forum…
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Hi Uwe, I'm afraid I've been no longer active in the Prerelease (nor in the Developer) forums for years now for various reasons.
Sadly, there is no property "z-index" or "stacking_order" or so that could give me the appropriate index for each object on the spread directly. However, I perfectly understand that altering the z-index of an object by script would, in turn, make it necessary to re-stack all other items on the respective spread which could have unpredictable side effects.
Anyway: Given that the "allPageItems" array reflects the correct stacking order, then easy workarounds can be used eg to find out the relative position of two objects - i.e. which one is on top of which one - which is the point of stake in my current project.
Cheers, Rudi
Find more inspiration, events, and resources on the new Adobe Community
Explore Now