Export Apple Mail Messages and Notes to Text Files

Tags: , , , , ,

Change Log:

  • 1.00 January 31, 2010 INITIAL RELEASE OF SCRIPT

Dive in to the code and, if you have any suggestions, please let me know about them in the comment thread below.

And remember:

  1. This script is being provided for the benefit of the Mac community. While it works for me, I cannot take responsibility for any data you might lose (or if your laptop experiences a Susan Boyle-esque meltdown…);
  2. “Community” means “participation”. Please share with the rest of us *how* you use these scripts in your workflow, how you think they could be improved (and feel free to help someone if they don’t understand something!)

(*
http://veritrope.com
Apple Mail -- Text File Exporter
Version 1.0
January 31, 2010

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/tips/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.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 0) then
            set theMessages to selection
            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 & theFileName & ".txt")
                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

CLICK HERE TO DOWNLOAD

Was this script helpful to you? Please Donate!


Found a bug in the script?

Click Here To Submit Your Bug Report!

Interested in what other AppleScripts are available on Veritrope.com?

Click here to see the latest list!

Export Apple Mail Messages and Notes to Text Files

Pages: 1 2

6 Comments / Jump to comment form

6 Responses to “Export Apple Mail Messages and Notes to Text Files”

  1. Twitter Comment


    Thanks to @veritrope for knocking up AppleScript to export Notes to text files from Mail.app [link to post]

    Posted using Chat Catcher

    • D D says:

      Thanks for putting this together. Two things: 1) Can I have the original dates exported with the notes? 2) The script fails because it requires Growl which I don’t have installed (I don’t really have a need for Growl). Do you have a version of the script that doesn’t need Growl?

      • Justin Justin says:

        The Growl issue must be a bug — it’s supposed to detect whether or not you have it installed and adjust. Click here to file a bug report so that I don’t lose track of it and, when I’m working on a new revision, I’ll spend some time on this!

        A question: How would you want the original dates presented? As a header at the top of the note?

        • lydia lydia says:

          for me, it would be useful to have basic messages headers placed at the top of each txt file. would love to see at least “From:”, “Subject:”, and “Date:”. would that be easy to add if i knew what i was doing?

          (i am about to take off on a big trip, am trying to create an archive of the relevant mail folder, don’t really have time to delve into learning applescript just now.)

  2. Danny Danny says:

    You just saved me about an hour. Thank you.

rss Subscribe to Comments RSS Feed


Trackbacks/Pingbacks

  1. Snow Leopard Issue With Mail, Exchange, and AppleScript rule | Veritrope
  2. AppleScript, MacHeist, Scottevest, Tavo, Hella, and Schneier | Veritrope

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>