InDesign: can "all page items" be narrowed down?

In InDesign I use the AS code:

set allFrames to all page items

to go through all the items of my open ID document to find the one with a specific Script Label I created. But as you’d expect, “all page items” literally goes through ALL page items which takes some time.

I really want it to only limit that code line search to either 1) only visible items, or 2) only items on the currently selected layer. How should I alter that code, and hopefully shave off some processing time?

Hi. Unless your document is exceedingly large or complex, there’s unlikely to be a significant processing time expenditure on iterating page items, but you can certainly narrow them by class or by addressing a specific object. Since you’re looking for a frame, you want rectangles. Note that using a whose clause to filter items may introduce a negligible processing expense of its own.

tell application "Adobe InDesign CS3"'s document 1
	tell active layer to if visible then rectangles --all frames on visible layer
	page 1's rectangles whose locked is false and content type is graphic type --editable images by page
	(rectangles whose label is "something")'s item 1 --the named item
end tell

Hm! Thanks, I’ll try that.

I should also add that the reason this is giving me trouble is I have two objects with the same script label, but they’re on different layers and the layer not in use is always invisible. So far no matter what I’ve tried so far it keeps accessing that layer I turned off because it’s on top of the layer I want and I assume it accesses layer in order.

You can use this sort of thing:

tell application id "com.adobe.InDesign" -- Adobe InDesign
	tell document 1
		set theLayer to layer "XYZ"
		set theItem to item 1 of all page items whose label is "Label" and item layer is theLayer
	end tell
end tell

In this approach the list of all page items isn’t returned from InDesign, and that’s the part that otherwise can take a while.

Oh I like that too. Thanks!

Good document setup saves time, and it’s best practice to use unique labels. I’m assuming you’re using the layer for versioning; unless this a one-off, consider appending that frame with a letter or number to differentiate.

Correct. But it’s not my document and I have no control over the layer names themselves, unfortunately.