Traverse on PageItems how they visually display in layers

Contributor ,
Mar 25, 2022 Mar 25, 2022

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?

TOPICS
How to , Scripting

Views

123

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Mar 25, 2022 Mar 25, 2022
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) }; };

Likes

Translate

Translate
Adobe Community Professional ,
Mar 25, 2022 Mar 25, 2022

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 )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 25, 2022 Mar 25, 2022

Copy link to clipboard

Copied

Thank you @Laubender 

Is it not possible with the property of pageItems instead of allPageItems?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 25, 2022 Mar 25, 2022

Copy link to clipboard

Copied

Reliably? Without further ado? Don't think so…

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 25, 2022 Mar 25, 2022

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? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 25, 2022 Mar 25, 2022

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..

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 25, 2022 Mar 25, 2022

Copy link to clipboard

Copied

LATEST

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)
    };   
};   

 

 

Screen Shot 11.pngScreen Shot 12.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines