export a grep search as text or .csv

I need to export following grep search to text or a .csv file:

\d{3}[-]\d{3}[-]\d{3}

which basically finds any digit in this format 000-000-000 & also need export the name of the document

Is that possible? I have to this to thousands of documents. The numbers are in another layer that contain another text elements. I have a script that exports layers to PDF but I want ONLY pattern 000-000-000 and export as text

I don’t understand your question. What is formatted like this 000-000-000 ? File Names? Contents of a file?

adayzdone,
Basically I’m working in a catalog & each InDesign CS4 document contains a sku number formatted
000-000-000. The catalog contains prices, descriptions, etc. The only thing I’m interested is to find all instances 3 digits followed by 2 hyphens & export that info into a text or csv. document with the name of the document.

Example:
“Braces Page 2” (name of document)
859-452-002 sku#
859-666-789 sku#
849-789-456 sku#
256-963-852 sku#

Braces Page 3"
123-456-789 sku#
123-963-852 sku#
741-852-963 sku#

The grep search \d{3}[-]\d{3}[-]\d{3} finds each instance but I know it’s not the best route because for each instance found I have to copy & paste it into an Excel document. How can you script to find that pattern like that? I know it is complicated but that’s why I’m looking for guidance here. The sku’s are in layer called z1 that contains more info.

Assuming you have an open document does this work for you.

tell application "Adobe InDesign CS5"
	--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{3}[-]\\d{3}"
	set myFinds to find grep the active document
	display dialog name of active document as text
	repeat with thisFind in myFinds
		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
end tell

If it does then all you need do is open a file for access and write the data. Make a loop for all your documents. :slight_smile:

Any problems shout back I do tons of this stuff in ID.

Edit: I quickly added a basic write to file couldn’t see anything that requires CSV. :slight_smile:

tell application "Adobe InDesign CS5"
	-- Clear the find grep/change grep preferences before each find/change operation.
	set find grep preferences to nothing
	set change grep preferences to nothing
	-- Escape your back slashes
	set find what of find grep preferences to "\\d{3}[-]\\d{3}[-]\\d{3}"
	-- Do the find in open document
	set myFinds to find grep the active document
	-- Set up a sting to add to. ( doc must be saved )
	set myData to (name of active document as text) & return
	-- display dialog name of active document as text
	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
end tell

display dialog myData

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

write_to_file(myData, thisFile, false)

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

Magnificent!:smiley: Mark67

I’m curious why the expression:

\d{3}[-]\d{3}[-]\d{3}
does not work in applescript & but it does work in the grep search within InDesing?

Or viceversa
\d{3}[-]\d{3}[-]\d{3} works in applescript but not in InDesign?

You are passing a script string to the expression. In the script string you need to escape the backslash so.

replace single \ with double \ when passing from script. Hence the comment.

– Escape your back slashes :smiley:

Glad it’s got you out the blocks anyhows. Should be easy to adapt now me thinks.

Mark67,
Thanks for all your the help. Like you mentioned your script works only with open ID files it’s an excellent start. With your script as a guide & my little experience I needed to have adapted to choose a folder with ID files, open, find grep, close & write the results of EACH InDesign file opened. The problem I’m having is generating the results of the last file open by the script. Example the script open 5 ID files, I should have on the text file the 5 names of the documents & the results of the grep search. In what of part of the script I’m failing to write the results of the grep search of all ID files opened?

set source_folder to choose folder 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
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{3}[-]\\d{3}(?!\\d)"
			-- Finds 000-000-000 patterns (example 895-963-789, 456-748-123) *** 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 myFinds to find grep the active document
			
			-- Set up a string to add to. ( doc must be saved )
			set myData to (name of active document as text) & return
			
			-- display dialog name of active document as text
			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
			
			close document 1 saving no
		end tell
		
	end if
end repeat



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

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

beep 3
display dialog "The script is done. Go to your desktop & open SKUs.tx file" buttons {"OK"} default button 1 giving up after 60

From a quick glance. You need to move 2 lines. Shift this up to the top before the loop.

–write results to “SKUs.txt”
set thisFile to (path to desktop as text) & “SKUs.txt”

then the call to the sub-routine needs to be inside the loop. You now also want to change the last parameter to true that way new data is added to end. :slight_smile:

write_to_file(myData, thisFile, true)

Done

I changed everything that was false to true.

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 true 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 true
	end try
end write_to_file

I got stock in that one.

Since yesterday I have been trying to figure it out how to make the script work & I know I’m close but I have to rely on the professionals to finish it. The problem I’m having is generating the results of the last file open by the script & needs to write the name of document & the grep results of all ID documents chosen. Here’s what I have so far. Please advise

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{3}[-]\\d{3}(?!\\d)"
			-- Finds 000-000-000 patterns (example 895-963-789, 456-748-123) 
			--*** 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 myFinds to find grep the active document
			-- Set up a string to add to. ( doc must be saved )
			set myData to (name of active document as text) & return
			
			
			-- display dialog name of active document as text
			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
			
			close document 1 saving no
		end tell
	end if
end repeat


--============================
write_to_file(myData, thisFile, true)
-- Last parameter, true ===to write new data data to the end

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 true 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 true
	end try
end write_to_file

Almost there this any better. Tested on just 3 docs.

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 CS5"
			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{3}[-]\\d{3}(?!\\d)"
			-- Finds 000-000-000 patterns (example 895-963-789, 456-748-123) 
			--*** 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 myFinds to find grep the active document
			-- Set up a string to add to. ( doc must be saved )
			set myData to (name of active document as text) & return
			
			
			-- display dialog name of active document as text
			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
			
			-- 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

:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D Perfecto!

I see where it was the error. I tried that before closing of the document in the main loop

write_to_file(myData, thisFile, true)
close document 1 saving no

but if I don’t add the word “my” was never going to work

my write_to_file(myData, thisFile, true)

That was something I wasn’t be going to able to figure it out with the xperience I have. Great Forum & thank you MARK67 for taking the time & experience to guide me. :lol:

I forget the my or of me. all the time myself. :slight_smile: So you could use

my write_to_file(myData, thisFile, true)

or

write_to_file(myData, thisFile, true) of me

Glad it worked all the same.