InDesign -- Set active page?

I have a script which iterates through the text in a story (in linked text frames). I would like InDesign to change the active page displayed so I can observe the action of the script as it goes through the text. However, the script below always returns “1” as the parent page (pName) of the text frame, and not the page where the script action is taking place. Is there a way to find out the “real” page where the script has selected text? I’ve used examples from Shirley Hopkins’ book. Here’s the relevant portion of my script below (the part that is commented out after “save” is where I was trying to figure this thing out):

repeat with j from 1 to x
set saveCounter to saveCounter + 1
if saveCounter > 25 then – save the document every 25th iteration
set saveCounter to 1
tell docRef
save
(* tell docRef
activate
set frameRef to my frameRefOfSelection()
set pName to name of parent page of frameRef
set active page of window 1 to page pName
zoom window 1 of docRef given fit page
end tell *)
end tell
end if
– do other stuff
end repeat

– HANDLERS

(Returns reference to active document; otherwise generates error.)
on getDocRef()
tell application “Adobe InDesign CC 2014”
if modal state = true then
error “Please close dialogs before running script”
end if
if not (exists document 1) then
error “Requires active document”
else
return document 1
end if
end tell
end getDocRef


(Returns reference to selected text frame or text frame of selected text. Throws error if no valid selection.)
on frameRefOfSelection()
set errStr to “Requires text or text frame selection.”
tell application “Adobe InDesign CC 2014”
set selList to selection
if length of selList > 0 then
if class of item 1 of selList is in {text, insertion point} then
set storyRef to parent of item 1 of selList
set frameRef to item 1 of text containers of storyRef
else if class of item 1 of selList = textFrame then
set frameRef to item 1 of selList
else
error errStr
end if
else
error errStr
end if
end tell
return frameRef
end frameRefOfSelection

Hi ChuckH1,

tell application "Adobe InDesign CS5"
	set paginaRef to object reference of page imgNomePagina of active document
	set riferimentoBox to page item id boxID of active document
	set selection to riferimentoBox
	tell layout window 1
		set active page to paginaRef
		zoom given fit page
	end tell
end tell

Ame - Stefano

Hi. Your example script appears to return the same page because the subroutine frameRefOfSelection doesn’t iterate through text containers”it just uses the first one, whose page is static. The “real” page, in this case, is the respective text container’s parent’s name.

Marc, thanks. I’m not sure how to find the relevant text container. I assume I can ask for the parent page of that? I am able to get a list of all the text containers (it’s a long list with entries like: text frame id 53898 of spread id 53892 of document id 1, text frame id 63252 of spread id 63246 of document id 1) but how do I determine which one is associated with the selected text? Thanks.

Actually, you want the story that houses the containers. You should be able to edit this to your needs:


tell application "Adobe InDesign CS3"'s document 1
	repeat with aFrame in (get selection's parent story's text containers) --assumed is selected = text frame
		set layout window 1's active page to page (aFrame's parent's name)
		zoom layout window 1 given fit page
		delay 1 --so you can see it
	end repeat
end tell

Marc, thanks. I tweaked it a bit and this seems to work with a text selection (what I needed, rather than a text frame selection):

set docRef to getDocRef()
tell application "Adobe InDesign CC 2014"
	tell docRef
		set theRef to object reference of selection
		set theItem to item 1 of theRef
		set parFrm to object reference of parent text frames of theRef
		set layout window 1's active page to page (parFrm's parent page's name)
		zoom layout window 1 given fit page
	end tell
end telll