Collect the text of selected e-mails in Mail.app

Dec 08, '10 07:30:00AM

Contributed by: deef

I have several hundred e-mails which would be better dealt with as a single document. I wrote this Applescript to put the text of all selected e-mails in Mail into a new TextEdit .rtf document.

It also deletes the text 'Begin forwarded message:' to clean things up and includes a find and replace routine from MacScripter so it can be easily customized.

Here is the script itself:

tell application "Mail"
 set selectedMessages to selection
 if (count of selectedMessages) is equal to 0 then
  display alert "No Messages Selected" message "Select the messages you want to collect before running this script."
 end if
 set theText to ""
 repeat with theMessage in selectedMessages
  set theText to theText & (content of theMessage) as string
 end repeat
end tell

on replaceText(find, replace, subject) -- from MacScripter
 set prevTIDs to text item delimiters of AppleScript
 set text item delimiters of AppleScript to find
 set subject to text items of subject
 
 set text item delimiters of AppleScript to replace
 set subject to "" & subject
 set text item delimiters of AppleScript to prevTIDs
 
 return subject
end replaceText

set theText to replaceText("Begin forwarded message:", "", theText)

tell application "TextEdit"
 set theDocument to make new document
 set text of theDocument to theText
end tell
Installation: Usage:
[crarko adds: I tested this, and it works as described.]

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101204122006460