Description
- This AppleScript will export any selected Messages or Notes in Apple Mail as individual text files to a temporary folder on your Desktop (called "Temp Export Folder");
- The script also attempts to remove any "illegal characters" from the file name as to avoid the process grinding to a complete halt!
- If you have Growl installed, the script should detect it and notify you when your export is completed.
The Code
(*
http://veritrope.com
Apple Mail -- Text File Exporter
Version 1.1
January 17, 2011
Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/export-apple-mail-messages-and-notes-to-text-files
Installation: Just double-click on the script!
FastScripts Installation (Optional, but recommended):
--Download and Install FastScripts from http://www.red-sweater.com/fastscripts/index.html
--Copy script or an Alias to ~/Library/Scripts/Applications/Mail
--Set up your keyboard shortcut
CHANGELOG:
1.10 ADDED INCREMENTAL COUNTER FOR MESSAGES WITH SAME TITLES (THANKS HANS ERIK!)
1.00 INITIAL RELEASE
*)
property growlName : "MailTextExport"
property appName : "Mail"
property successCount : "0"
(* CHECK FOR GROWL *)
tell application "System Events"
set processnames to name of every process
end tell
if appIsRunning("GrowlHelperApp") then
set isRunning to true
tell application "GrowlHelperApp"
set allNotificationsList to {"Success Notification", "Failure Notification"}
set enabledNotificationsList to {"Success Notification", "Failure Notification"}
register as application growlName all notifications allNotificationsList default notifications enabledNotificationsList icon of application appName
end tell
end if
(*MAIN PROGRAM *)
(*TEMP FILES PROCESSED ON THE DESKTOP*)
tell application "Finder"
if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
if not (the folder "Temp Export Folder" exists) then
set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
end if
set SaveLoc to FolderLoc as string
end tell
(*APPLE MAIL ITEM SELECTION *)
tell application "Mail"
try
if (selection is not {}) then
set theMessages to selection
set msgnr to 1
repeat with thisMessage in theMessages
set myTitle to the subject of thisMessage
set myContent to the content of thisMessage as text
set ReplyAddr to the reply to of thisMessage
set EmailDate to the date received of thisMessage
set theSource to the source of thisMessage
(*CLEAN TITLE FOR FILE *)
set previousDelimiter to AppleScript's text item delimiters
set potentialName to myTitle
set legalName to {}
set illegalCharacters to {".", ",", "/", ":", "[", "]"}
repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as text
if thisCharacter is not in illegalCharacters then
set the end of legalName to thisCharacter
end if
end repeat
set AppleScript's text item delimiters to ""
if length of legalName is greater than 32 then
set legalName to items 1 thru 32 of legalName as text
else
set legalName to legalName as text
end if
set AppleScript's text item delimiters to previousDelimiter
set theFileName to legalName
(*EXPORT TO TXT FILE *)
set theText to myContent
set theFilePath to (SaveLoc & msgnr & "_" & theFileName & ".txt")
set msgnr to msgnr+1
set theFileReference to open for access theFilePath with write permission
write theText to theFileReference as «class utf8»
close access theFileReference
set successCount to successCount + 1
end repeat
(* GROWL *)
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL SUCCESS
notify with name ¬
"Success Notification" title ¬
"Export Success" description "Successfully Exported " & successCount & ¬
" Messages to Export Folder!" application name growlName
end tell
set successCount to 0
end if
else if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
notify with name ¬
"Failure Notification" title ¬
"Export Failure" description ¬
"No Messages or Notes Selected!" application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR NO SELECTION
display dialog "No Messages or Notes Selected!" with icon 0
end if
(* ERROR HANDLING *)
on error errText number errNum
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
notify with name ¬
"Failure Notification" title ¬
"Import Failure" description "Failed to export " & return & myTitle & ¬
"" due to the following error: " & return & errText ¬
application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR ERROR
display dialog "Item Failed to Export: " & errNum & return & errText with icon 0
end if
end try
end tell
(* SUBROUTINES *)
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
--REPLACE SUBROUTINE
on replaceString(theString, theOriginalString, theNewString)
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
set theStringParts to text items of theString
if (count of theStringParts) is greater than 1 then
set theString to text item 1 of theStringParts as string
repeat with eachPart in items 2 thru -1 of theStringParts
set theString to theString & theNewString & eachPart as string
end repeat
end if
set AppleScript's text item delimiters to od
return theString
end replaceString
http://veritrope.com
Apple Mail -- Text File Exporter
Version 1.1
January 17, 2011
Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/export-apple-mail-messages-and-notes-to-text-files
Installation: Just double-click on the script!
FastScripts Installation (Optional, but recommended):
--Download and Install FastScripts from http://www.red-sweater.com/fastscripts/index.html
--Copy script or an Alias to ~/Library/Scripts/Applications/Mail
--Set up your keyboard shortcut
CHANGELOG:
1.10 ADDED INCREMENTAL COUNTER FOR MESSAGES WITH SAME TITLES (THANKS HANS ERIK!)
1.00 INITIAL RELEASE
*)
property growlName : "MailTextExport"
property appName : "Mail"
property successCount : "0"
(* CHECK FOR GROWL *)
tell application "System Events"
set processnames to name of every process
end tell
if appIsRunning("GrowlHelperApp") then
set isRunning to true
tell application "GrowlHelperApp"
set allNotificationsList to {"Success Notification", "Failure Notification"}
set enabledNotificationsList to {"Success Notification", "Failure Notification"}
register as application growlName all notifications allNotificationsList default notifications enabledNotificationsList icon of application appName
end tell
end if
(*MAIN PROGRAM *)
(*TEMP FILES PROCESSED ON THE DESKTOP*)
tell application "Finder"
if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
if not (the folder "Temp Export Folder" exists) then
set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
end if
set SaveLoc to FolderLoc as string
end tell
(*APPLE MAIL ITEM SELECTION *)
tell application "Mail"
try
if (selection is not {}) then
set theMessages to selection
set msgnr to 1
repeat with thisMessage in theMessages
set myTitle to the subject of thisMessage
set myContent to the content of thisMessage as text
set ReplyAddr to the reply to of thisMessage
set EmailDate to the date received of thisMessage
set theSource to the source of thisMessage
(*CLEAN TITLE FOR FILE *)
set previousDelimiter to AppleScript's text item delimiters
set potentialName to myTitle
set legalName to {}
set illegalCharacters to {".", ",", "/", ":", "[", "]"}
repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as text
if thisCharacter is not in illegalCharacters then
set the end of legalName to thisCharacter
end if
end repeat
set AppleScript's text item delimiters to ""
if length of legalName is greater than 32 then
set legalName to items 1 thru 32 of legalName as text
else
set legalName to legalName as text
end if
set AppleScript's text item delimiters to previousDelimiter
set theFileName to legalName
(*EXPORT TO TXT FILE *)
set theText to myContent
set theFilePath to (SaveLoc & msgnr & "_" & theFileName & ".txt")
set msgnr to msgnr+1
set theFileReference to open for access theFilePath with write permission
write theText to theFileReference as «class utf8»
close access theFileReference
set successCount to successCount + 1
end repeat
(* GROWL *)
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL SUCCESS
notify with name ¬
"Success Notification" title ¬
"Export Success" description "Successfully Exported " & successCount & ¬
" Messages to Export Folder!" application name growlName
end tell
set successCount to 0
end if
else if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
notify with name ¬
"Failure Notification" title ¬
"Export Failure" description ¬
"No Messages or Notes Selected!" application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR NO SELECTION
display dialog "No Messages or Notes Selected!" with icon 0
end if
(* ERROR HANDLING *)
on error errText number errNum
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
notify with name ¬
"Failure Notification" title ¬
"Import Failure" description "Failed to export " & return & myTitle & ¬
"" due to the following error: " & return & errText ¬
application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR ERROR
display dialog "Item Failed to Export: " & errNum & return & errText with icon 0
end if
end try
end tell
(* SUBROUTINES *)
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
--REPLACE SUBROUTINE
on replaceString(theString, theOriginalString, theNewString)
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
set theStringParts to text items of theString
if (count of theStringParts) is greater than 1 then
set theString to text item 1 of theStringParts as string
repeat with eachPart in items 2 thru -1 of theStringParts
set theString to theString & theNewString & eachPart as string
end repeat
end if
set AppleScript's text item delimiters to od
return theString
end replaceString
Hi,
I have also added a couple of lines that add the recipients and cc:recipients to the exportfile. Interested?
In fact, the a kind of header is added, containing 3 parts:
1. title (subject)/reply/sender/date
2. recipients list
3. cc:recipients list
Sections are separated by a return and a line of dashes.
It does add some overhead, so the script may take longer to run.
You bet!
Use the Code Submission Page to send your additions. Once I receive them, I’ll merge them into the code above.
Hi Justin,
I posted the additional scriptlines.
One question: do you if it’s possible to export only the text-part of an email message? I did look into the dictionary, but I did not find a hint in the specs.
I believe any binary component (a graphic, attachment etc.) is in fact part of the message body and exported just along with the rest…
regards,
Hans Erik
Hans,
1.) I gave your script its own page so that people could get your code easily!
2.) Using the terms from Mail’s AppleScript Dictionary, the “content” of a message is the plaintext. The “source” is the raw data, which includes header and HTML info. Attachments are actually separate from these two — referred to as “Mail Attachments”.
Justin – your script is a real jem! Just what the doctor ordered!!
I only wish I came across this a couple of years back, when you first released it.
I appreciate you putting this out there.
10/10!!
Warm regards,
John
Thanks John – much appreciated!
Hey mate,
AWESOME script. I’ve tried to modify it to have the date printed in the title of the output file, but to no avail. Turns out I suck at scripting…
Any ideas? I’m looking to have the output be something like “3/12/12 – Note title”
Cheers mate.
Glad you like it, Michael!
I’m a bit busy, so I don’t have time right now to show you how to do this…. but it is totally possible and there are some good resources available to get you going. Might I suggest a quick bit of Google-fu with the words “AppleScript” and “coerce date to text”?
Dive in and play with some of the examples you find… think you’ll get the hang of it quickly!
I’ve tried adding stuff like this to the script, played around with trying to change the date into text, but just can’t do it.
set {year:y, month:m, day:d, hours:h, minutes:min} to (msgDate)
set msgDate to ("" & y & my pad2(m as integer) & my pad2(d))
I understand you’re busy so it’s all good. Thanks anyway!! 🙂
Thank you very much for saving me many hours of labor. For some reason I’m not able to make a donation. When I click on “Donate” nothing happens. What am I doing wrong?
It’s not you… it’s me!
There was a WordPress plugin that stopped working and which was blocking the Donate button. Should be fixed… but the only way to tell for sure is for all of you to try it! 😉
(Many thanks, Ron!)