INDESIGN - Reference to all boxes in document

I am trying to name all the boxes on each page of sequentially.

For instance 2 pages 28 boxes on each.
Number sequence
1 (top right page 1)
56 (bottom left page 2).

I guess I am at a loss on how to reference all the boxes in the document
and number them as stated above.

The following script will number 1 to 28 on each page

on revbox(theboxes)
set theboxes to reverse of theboxes
return theboxes
end revbox
tell application “InDesign CS”
tell document 1
repeat with i from 1 to count of pages
tell page i
set theboxes to (every item of all page items where class of it = rectangle and label is “EPS”)
set theboxes to revbox(theboxes) of me
repeat with i from 1 to count of theboxes
set label of item i of theboxes to “EPS” & “-” & i as string
end repeat
end tell
end repeat
end tell
end tell

on MySort(MyRectangles)
	return MyRectangles
end MySort

tell application "InDesign CS"
	tell document 1
		set MyCounter to 0
		repeat with i from 1 to count of every page
			set MyRectangles to (every item of all page items where class of it = rectangle and label is "EPS")
			tell me to set MyRectangles to MySort(MyRectangles)
			repeat with i from 1 to count of MyRectangles
				set MyCounter to MyCounter + 1
				set label of item i of MyRectangles to "EPS" & "-" & i as string
			end repeat
		end repeat
	end tell
end tell

This should work, notice that I changed your handler to reverse the list of boxes. InDesign returns a list of boxes based on their index or stacking order, not left to right/top down. While simply reversing the list may work in some cases , once one of the boxes is brought foreword, send back, or a new box is created then it will throw everything off. To fix this you will need to build an expanded sort routine wich will sort the boxes based off of their origins.

Hi Jerome:

Must be Monday:)

I previously indicated that
1 (top right page 1)
56 (bottom left page 2).

I should have indicated
1 (top left page 1)
56 (bottom right page 2).

When I run your script the starting number on the top left of page 1 is 28 and the starting number on the top left of page 2 is 56.
How do I reverse this order.

If you add back in your line to reverse the entries then you have:

on MySort(MyRectangles)
	set MyRectangles to reverse of MyRectangles
	return MyRectangles
end MySort

tell application "InDesign CS"
	tell document 1
		set MyCounter to 0
		repeat with i from 1 to count of every page
			set MyRectangles to (every item of all page items where class of it = rectangle and label is "EPS")
			tell me to set MyRectangles to MySort(MyRectangles)
			repeat with i from 1 to count of MyRectangles
				set MyCounter to MyCounter + 1
				set label of item i of MyRectangles to "EPS" & "-" & i as string
			end repeat
		end repeat
	end tell
end tell

Now to ensure that you always have the rectangles organized from left to right/top to bottom then you would need to sort them based off of their origins (item 1 and item 2 of the geometric bounds) otherwise you might get some of them that are out of order becouse InDesign returns the list of rectangles based on the index of the box, or the top most of the stacking order to the bottom most and not based off of the origin of the box.

OK I tried that script but the result was that “EPS-1” is top left on page 2 and “EPS-29” is top left on page 1.
I noticed a variable myCounter in your script but it was not used.

Here is a modified version with the variable myCounter being used.
This now works in labeling all the boxes in sequence.

Thanks for all your help Jerome.

on MySort(MyRectangles)
	set MyRectangles to reverse of MyRectangles
	return MyRectangles
end MySort

tell application "InDesign CS"
	tell document 1
		set myCounter to 0
		repeat with aPAGE from 1 to count of every page
			tell page aPAGE
				set MyRectangles to (every item of all page items where class of it = rectangle and label of it is "EPS")
				tell me to set MyRectangles to MySort(MyRectangles)
				repeat with i from 1 to count of MyRectangles
					set label of item i of MyRectangles to "EPS" & "-" & (i + myCounter) as string
				end repeat
				set myCounter to (myCounter + (count of MyRectangles))
			end tell
		end repeat
	end tell
end tell