extract email to CSV or tab delimited?

Hi,

I’d like to extract a selected email (or all selected email) in a mailbox (OS X Mail.app) to a comma delimited or tab delimited text file.

Since the bodies of email contain commas, tabbed might be better, but I’m not particular either way as long as it works.

Any help with this is much appreciated, and I don’t care if the script is quick and dirty.

Would it be possible to post some of the AppleScript code you have so far, especially the parts you are having trouble with? It makes it a lot easier for us to see how we can help.

This should get you started:

Jon

Jon,

WOW!
THANK YOU!

That’s awesome!

I have one question-

The script gets the Message ID, Sender, Subject, etc. sorted properly, but when it comes to the body of the message, The first line of content appears correctly, but each new line in the content ends up as a new row under message ID.

I did have to insert some spaces to satisfy the script editor when copying and pasting- did I mess it up somehow?

Man, this is so close to pefect! Thanks!

Here is a slightly modified version of the code that strips new line characters and replaces with a delimiter that you can set in the properties at the top of the script. It is currently set to “” You can use the search_and_replace handler to swap any other problem text as necessary or desired:

Jon

I tried to take my own approach to it, and it worked pretty well. I see now that it is much the same as what you’re doing.

Thanks again! You have no idea how helpful this is!

property item_delim : “,”
property record_delim : return

– this makes sure we generate a unique text file
set the_num to 1
repeat
try
set file_path to (path to desktop as string) & "Saved Mail " & (the_num as string) & “.csv”
get file_path as alias
set the_num to the_num + 1
on error
exit repeat
end try
end repeat

set the_headers to (“” & ¬
“message id” & item_delim & ¬
“sender” & item_delim & ¬
“date sent” & item_delim & ¬
“recipient(s)” & item_delim & ¬
“cc recipient(s)” & item_delim & ¬
“bcc recipient(s)” & item_delim & ¬
“subject” & item_delim & ¬
“content” & record_delim)
my write_to_file(file_path, the_headers)

tell application “Mail”
set the_messages to selection
repeat with i from 1 to count of the_messages
set the_message to (a reference to item i of the_messages)
set the_message_id to (message id of the_message)
set the_sender to (sender of the_message)
set the_date_sent to (date sent of the_message)
set the_recipient_rec to (recipient of the_message)
set the_cc_recipient_rec to (cc recipient of the_message)
set the_bcc_recipient_rec to (bcc recipient of the_message)
set the_subject to (subject of the_message)
set the_content to (my replace_chars(content of the_message as string, “”“, “””"))

	set the_recipient to my return_names(the_recipient_rec)
	set the_cc_recipient to my return_names(the_cc_recipient_rec)
	set the_bcc_recipient to my return_names(the_bcc_recipient_rec)
	
	set return_string to ("" & ¬
		""" & the_message_id & """ & item_delim & ¬
		""" & the_sender & """ & item_delim & ¬
		""" & (the_date_sent as string) & """ & item_delim & ¬
		""" & the_recipient & """ & item_delim & ¬
		""" & the_cc_recipient & """ & item_delim & ¬
		""" & the_bcc_recipient & """ & item_delim & ¬
		""" & the_subject & """ & item_delim & ¬
		""" & the_content & """ & record_delim)
	
	-- do something with return_string like add it to a FMP DB or write it to a text file, etc. 
	my write_to_file(file_path, return_string)
end repeat

end tell

on return_names(the_rec)
tell application “Mail”
set return_string to “”
set record_count to (count of the_rec)
repeat with i from 1 to record_count
set the_name to name of (item i of the_rec)
set the_address to address of (item i of the_rec)
if the_name is not the_address then set the_name to the_name & “<” & the_address & “>”
set return_string to return_string & the_name
if i < record_count then set return_string to return_string & ", "
end repeat
return return_string
end tell
end return_names

on write_to_file(file_path, the_string)
try
open for access file file_path with write permission
write the_string to file file_path starting at eof
close access file file_path
on error
try
close access file file_path
end try
end try
end write_to_file

on replace_chars(this_text, search_string, replacement_string)
set AppleScript’s text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript’s text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript’s text item delimiters to “”
return this_text
end replace_chars

– this script was automatically tagged for
– color coded syntax by Script to Markup Code
– written by Jonathan Nathan