Creating "save as" functionality to .eml file in AppleScript

Posted by unieater on Super User See other posts from Super User or by unieater
Published on 2012-10-07T11:58:29Z Indexed on 2012/10/08 3:39 UTC
Read the original article Hit count: 228

Filed under:
|
|
|

I'm new to AppleScript and trying to figure out how to save a Mail.app message as an .eml message. Ideally, I would like it to act similar to the Mail menu bar actions, in which it saves the message and the attachments together.

The workflow is that you have a selection in Mail, hit a hotkey and the function creates a filename (newFile) for the email to be saved. I just need help with how to save the message to the path (theFolder) in an .eml format.

tell application "Mail"
set msgs to selection

if length of msgs is not 0 then
    display dialog "Export selected message(s)?"
    if the button returned of the result is "OK" then

        set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles

        repeat with msg in msgs

            -- determine date received of msg and put into YYYYMMDD format
            set msgDate to date received of msg
            -- parse date SEMversion below using proc pad2()
            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))

            -- assign subject of msg
            set msgSubject to (subject of msg)

            -- create filename.eml to be use as title saved
            set newFile to (msgDate & "_" & msgSubject & ".eml") as Unicode text

            -- copy mail message to the folder and prepend date-time to file name

            -- THIS IS WEHRE I AM COMPLETE LOST HOW SAVE THE EMAIL into theFolder           
        end repeat

        beep 2
        display dialog "Done exporting " & length of msgs & " messages."
    end if -- OK to export msgs
end if -- msgs > 0
end tell

on pad2(n)
return text -2 thru -1 of ("00" & n)
end pad2

© Super User or respective owner

Related posts about osx

Related posts about automation