InDesign CC 2019 - Find GREP on specific page

I am trying to create an index of part numbers (always 6 digits) for each page of a catalog. Is there a way to run find GREP on each individual page of an InDesign document. Telling the page generates a “page doesn’t understand” error. I want to end up with a tab separated list with part numbers in the first position and page numbers in the second position. This is what I have. Any help would be greatly appreciated.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set niceList to {}
set finalList to {}
set pageList to {}
tell application "Adobe InDesign CC 2019"

	set find grep preferences to nothing
	set change grep preferences to nothing
	set find what of find grep preferences to "\\d{6}"
	set applied paragraph style of find grep preferences to nothing

	set theDoc to the active document
	tell theDoc
		set thePages to pages of theDoc
		repeat with i in thePages
			set end of pageList to name of i
		end repeat
		repeat with j from 1 to count of items of pageList
			set theList to ""
			
			tell page j of theDoc
				
				set theList to find grep
				repeat with i in theList
					set end of niceList to ((text of i as string) & tab & (item j of pageList as string))
				end repeat
			end tell
		end repeat
	end tell
end tell

Browser: Safari 537.36
Operating System: macOS 10.14

Hi. This mostly works, but you grep a page’s text objects, rather than the page itself.
change this: [format] tell page j of theDoc[/format]
to this: [format]tell theDoc’s page j’s text frames[/format]
You can preview the outcome in the editor by ending your code with “niceList,” and, since ASObjC commands aren’t used, you can also remove the first two “use” statements.

Thanks Marc - I tried that and got nothing because everything in my test spread is in a table (the whole project will be a mix of regular text frames and tables). I tried

tell theDoc's page j's tables

and got "every table of page 1 of document id 3 doesn’t understand the “find grep” message.

Ah. In that case, you have to reference the table’s parent—the text frame.
[format] change grep theDoc’s page j’s text frames’s tables
[/format]

If you’ve mixed tables and text inconsistently, then this become slightly more complex; you’ll need two different grep statements to handle both classes, and it will probably require a try block to not act when a table isn’t encountered.

Thank you Marc, it still gives an error
“every table of every text frame of page 1 of document id 40 doesn’t understand the “find grep” message.”, it gives a similar message when using change grep. Any ideas? I really appreciate your help

Hi. Your object doesn’t understand the command, as it doesn’t exist, as I stated would be a pitfall. The error from table non-existence needs to be trapped in a try block (or otherwise avoided). A frame may also not exist, but that’s A-okay by InDesign, as an empty list gets returned.

tell application "Adobe InDesign CS3"'s document 1
	repeat with Counter from 1 to count pages
		try
			find grep (page Counter's text frames's tables)
		end try
		find grep (page Counter's text frames)
	end repeat
end tell

Thank you Marc

Duplicating each page of the documents to a temp document to GREP is the solution that I’ve gone with. Here’s my final code for anyone else trying to do something similar. Thanks again Marc for your help.

set niceList to {}
set finalList to {}
global x, finalList
set AppleScript's text item delimiters to ""



set theFolder to choose folder with prompt "Please select a folder of InDesign files to process." 
set theFiles to list folder theFolder without invisibles

set fileCount to 0

set theFiles to my simple_sort(theFiles)

tell application "Adobe InDesign CC 2019"
	# • • • • • • • • • • # \\
	set find grep preferences to nothing
	set change grep preferences to nothing
	set find what of find grep preferences to "\\d{6}"
	set applied paragraph style of find grep preferences to nothing
	# • • • • • • • • • • # \\
	set find text preferences to nothing
	
	set case sensitive of find change text options to false
	set include footnotes of find change text options to false
	set include hidden layers of find change text options to false
	set include locked layers for find of find change text options to false
	set include locked stories for find of find change text options to false
	set include master pages of find change text options to false
	set whole word of find change text options to false
	
	tell script preferences
		set user interaction level to never interact
	end tell
end tell

repeat with x in theFiles
	
	
	set thisFile to theFolder & x as string
	
	if thisFile ends with ".indd" then
		
		set fileCount to (fileCount + 1)
		
		tell application "Adobe InDesign CC 2019"
			
			with timeout of 9999999 seconds
				open thisFile
			end timeout
			
			set theDoc to the active document
			set pageList to {}
			set thePages to ""
			set keepGoing to false
			tell theDoc
				set thePages to pages of theDoc
				repeat with i in thePages
					set end of pageList to name of i
				end repeat
				repeat with x from 1 to count of pageList
					tell application "Adobe InDesign CC 2019"
						set tempDoc to make new document
					end tell
					try
						duplicate every page item of item x of thePages to page 1 of tempDoc
						set keepGoing to true
					on error
						set end of finalList to "?" & "," & (item x of pageList as string)
					end try
					if keepGoing is true then
						tell application "Adobe InDesign CC 2019"
							with timeout of 9999999 seconds

							set find what of find grep preferences to "\\d{6}"
							
							tell tempDoc
								set testList to ""
								set testList to find grep
								
								repeat with i in testList
									set thisSet to ((text of i as string) & "," & (item x of pageList as string))
									if finalList does not contain thisSet then
										set end of finalList to thisSet
									end if
								end repeat
								close tempDoc saving no
								delay 10
							end tell
						end tell
					end if
				end repeat
				close theDoc saving no
			end tell
		end tell
	end if
end repeat

set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {","}}
tell application "Microsoft Excel"
	activate
	with timeout of 600 seconds
		set theBook to make new workbook
	end timeout
	set value of cell "A1" to "Part Numbers"
	set value of cell "B1" to "Page Number"
	set theSheet to active sheet of theBook
	set usedRange to used range of theSheet
	set rCount to count of rows of usedRange
	set rCount to (rCount + 1)
	repeat with i from 1 to (count of finalList)
		try
			set value of cell ("A" & (rCount as string)) to (text item 1 of item i of finalList) as string
			set value of cell ("B" & (rCount as string)) to (text item 2 of item i of finalList) as string
			set rCount to (rCount + 1)
		end try
	end repeat
end tell
set AppleScript's text item delimiters to saveTID


on simple_sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end simple_sort