Grep pattern search results per page

I’m trying to modify the script below to find the pattern (3 digit-7 digit) per page. Currently I’m getting pattern results per InDesign document but the ideal scenario is to have patterns results per each page.

Example: Document with more than 1 page:¨
ABC_S_150-151¨
ABS_R_125-127¨¨

the text results should be:
ABC_S_150-151.indd_150
¨260-409600¨
260-128628
¨260-128772
¨ABC_S_150-151.indd_151¨
260-704601¨
260-123456
¨260-789456¨¨
ABC_R_125-127.indd_125
¨260-456123¨
123-894561¨
123-123456¨
000-852963¨
ABC_S_125-127.indd_126
¨456-562325
¨456-741852
526-789456¨
ABC_S_125-127.indd_127
¨562-562785
¨852-741365¨

Obviously I’m looking for a solution but more than that is a brief explanation how to do it- – Any comments

set source_folder to choose file with prompt "Select folder containing Indesign Docs to have a Sku's report" with multiple selections allowed without invisibles

tell application "Finder" to set item_list to every item of source_folder

--write results to "SKUs.txt"
set thisFile to (path to desktop as text) & "SKUs.txt"

--loop thru all ID docs
repeat with this_item in item_list
	set doc_kind to kind of (info for this_item as alias)
	if doc_kind contains "Indesign" then
		--only ID documents
		tell application "Adobe InDesign CS4"
			activate
			set user interaction level of script preferences to never interact
			-- Clear the find grep/change grep preferences before each find/change operation.
			set find grep preferences to nothing
			set change grep preferences to nothing
			set find what of find grep preferences to "\\d{3}[-]\\d{6}(?!\\d)"
			-- Finds 000-000000 9 digit patterns (example 895-963789, 456-748123) 
			--*** the expresion (?!\\d) will make sure that phone numbers are not included on the search		
			
			open this_item
			-- Do the find on active document
			
			set x to count pages of the active document
			set thePageName to name of page x of active document
			
			-- If document have more than 1 page find grep per page
			set myFinds to find grep the thePageName
			-- Set up a string to add to. 
			set myData to (name of active document & "_" & thePageName as text) & return
			
			repeat with thisFind in myFinds
				set myData to myData & (thisFind as text) & return
				-- display dialog thisFind as text				
			end repeat
			-- Clear the find grep/change grep preferences after each find/change operation.
			set find text preferences to nothing
			set change text preferences to nothing
			set user interaction level of script preferences to interact with all
			
			
			-- Last parameter, true ===to write new data data to the end
			my write_to_file(myData, thisFile, true)
			
			close document 1 saving no
		end tell
	end if
end repeat


--============================
on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as string
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

Hi. You just need to target the grep to the text frames that live on any given page.


set foundOnPage to {}

tell application "Adobe InDesign CS3"'s document 1
	repeat with pageIndex from 1 to count pages
		try
			if not (find grep page pageIndex's text frames) = {} then set foundOnPage's end to pageIndex
		on error
			--contains no or textless frame(s)
		end try
	end repeat
end tell

foundOnPage

Thanks MarcAnthony for your response. I pasted your code but I’m still getting same results as before.

when open the text file this is what is currently showing
All pattern results are in page instead of 2

ABC_S_150-151¨.indd_150
260-409600¨
260-128628
¨260-128772
260-704601¨
260-123456
¨260-789456¨¨

instead of :
Pattern results in 2 pages

ABC_S_150-151¨.indd_150
260-409600¨
260-128628
¨260-128772
ABC_S_150-151.indd¨_151
260-704601¨
260-123456
¨260-789456¨¨

I’m pretty sure I’m missing something simple in the code. See below.

set source_folder to choose file with prompt "Select folder containing Indesign Docs to have a Sku's report" with multiple selections allowed without invisibles

tell application "Finder" to set item_list to every item of source_folder

--write results to "SKUs.txt"
set thisFile to (path to desktop as text) & "SKUs.txt"

--loop thru all ID docs
repeat with this_item in item_list
	set doc_kind to kind of (info for this_item as alias)
	if doc_kind contains "Indesign" then
		--only ID documents
		tell application "Adobe InDesign CS4"
			activate
			set user interaction level of script preferences to never interact
			-- Clear the find grep/change grep preferences before each find/change operation.
			set find grep preferences to nothing
			set change grep preferences to nothing
			set find what of find grep preferences to "\\d{3}[-]\\d{6}(?!\\d)"
			-- Finds 000-000000 9 digit patterns (example 895-963789, 456-748123) 
			--*** the expresion (?!\\d) will make sure that phone numbers are not included on the search		
			
			open this_item
			-- Do the find on active document
			set x to count pages of the active document
			set thePageName to name of page x of active document
			set myFinds to find grep the thePageName
			-- Set up a string to add to. 
			set myData to (name of active document & "_" & thePageName as text) & return
			
			set foundOnPage to {}
			tell application "Adobe InDesign CS4"'s document 1
				repeat with pageIndex from 1 to count pages
					try
						if not (find grep page pageIndex's text frames) = {} then set foundOnPage's end to pageIndex
					on error
						--contains no or textless frame(s)
					end try
				end repeat
			end tell
			foundOnPage
			
			
			repeat with thisFind in myFinds
				set myData to myData & (thisFind as text) & return
				-- display dialog thisFind as text				
			end repeat
			-- Clear the find grep/change grep preferences after each find/change operation.
			set find text preferences to nothing
			set change text preferences to nothing
			set user interaction level of script preferences to interact with all
			
			
			-- Last parameter, true ===to write new data data to the end
			my write_to_file(myData, thisFile, true)
			
			close document 1 saving no
		end tell
	end if
end repeat


--============================
on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as string
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

Hi. The result is the same because the same actions are performed as in your first example. You need to strike the original find grep in the amended script; yours targets the document. My variable foundonPage returns the correct values, but you are still iterating through myFinds. Additionally, you’ve nested my tell block in another tell; I can’t immediately discern if this double reference is anything more than a cosmetic problem, but I would change the second one to simply “tell document 1”.

wonderful, thank you Marc

Marc,
I’m trying to understand your point. Months have passed by & still haven’t been able to make it work. I had nested your tell block in another tell because I don’t how adapt your code to my script to get the results I need.

Did you implement the corrections? The crux of my point was that you were initiating your own malformed search (using find grep). A second search is then performed by my code, which, presumably, returns the right values. You then favor the variable in the incorrect search.

You should provide a search term and let my code direct and initiate the (only) search, then use it. Pseudocode follows.

tell application "Adobe InDesign CS3"
      -- Clear the find grep/change grep...
end
 
--insert my code from post 2 ("set" thru "end tell")

  repeat with thisFind in foundonpage --the variable could be anything, as long as the [i]if not[/i] line in my code corresponds
              --do whatever you want with [i]thisfind's contents[/i]              
           end repeat