Get page number of every paragraph based on style in InDesign CS

I’m trying to get every page number of a certain paragraph style. I know that this would usually be down automatically using InDesign’s CS built in indexing panel, but I’m building several complex indexes and it would help me to have the page numbers in the original Excel file so I can reparse it with InData to create these indexes.

I’ve whipped up a quick script and it works except it doesn’t follow the page order of the document it goes (I’m not staring on page 1) 23, 24, 25, 27, 26, 29, 28, 30 and on and on. Can anyone can see what I doing wrong?


tell application "InDesign CS"
	activate
	set myDoc to active document
	tell myDoc
		
		set myTextFrames to every text frame
		set pageCounter to 1
		set itemCounter to 1
		
		set pageNumber to name of every page of myDoc
		
		--repeat for every text frame
		repeat with i from 2 to (count of myTextFrames)
			tell text frame i
				try
					set myitems to (object reference of every paragraph whose name of applied paragraph style is "Item")
				end try
			end tell
			
			--repeat for every item
			repeat with p from 1 to (count of myitems)
				tell application "Microsoft Excel"
					activate
					set cellNumber to "E" & itemCounter & ":E" & itemCounter
					set value of cell cellNumber to (item pageCounter of pageNumber as string)
					
				end tell
				set itemCounter to itemCounter + 1
			end repeat
			set pageCounter to pageCounter + 1
		end repeat
		
	end tell
end tell

Many Thanks

Kieran

Hi Kieranj

This is just a guess can’t do any testing on this machine but it looks to me like you would need to repeat thru the pages using
a counter variable repeat loop, that should start at page 1 and continue…
it looks like your just getting every page and repeating thru text frames. i would start with repeating thru pages.
Hope this helps

Thanks for the tip, when I looked at the script again with fresh eyes I could see I was over complicating things.

Heres the finished script incase anyone needs to do anything similar


tell application "InDesign CS"
	activate
	
	set myDoc to active document
	set myPages to every page of myDoc whose name of applied master is "A-Master"
	set myitemCounter to 1
	
	repeat with p from 1 to count of myPages
		
		tell page p of myDoc
			
			tell text frame 1
				
				set myitems to (every paragraph whose name of applied paragraph style is "Item")
				
				repeat with i from 1 to count of myitems
					
					set myPageNumber to name of parent as string
					
					tell application "Microsoft Excel"
						activate
						set cellNumber to "E" & myitemCounter & ":E" & myitemCounter
						set value of cell cellNumber to (myPageNumber as string)
					end tell
					
					set myitemCounter to myitemCounter + 1
					
				end repeat
				
			end tell
			
		end tell
		
	end repeat
	
end tell

Kieran

Thanks to both of you for solving this perplexing problem. :slight_smile: