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