Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
    
Is the default Save as… command (and the .eml format behind Raw Message Source) not powerful enough? Do you want to use different file names, or what is your motivation here? –  Daniel Beck Oct 7 '12 at 12:48
    
Functionality is powerful enough, but doesn't provide the time efficiency as I want to do this in batches with a date stamp in filename. Unfortunately with date stamp modification, and if I do it 100 times and I waste at least 30 mins. Hoping it's a one button, and 30 mins of my life is saved each time. –  unieater Oct 7 '12 at 13:53
    
It might be easier using the existing Save as… and adding some batch renaming afterwards. Would that also be an acceptable solution? –  Daniel Beck Oct 7 '12 at 14:35
1  
On my machine, save as will only save the first of several selected messages. –  adayzdone Oct 7 '12 at 18:00
1  
I've noticed the same, the Save As solution from Mail doesn't give you a batch .eml save capability. –  unieater Oct 7 '12 at 19:57

1 Answer 1

You can copy the .emlx file from your Mail folder.

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 POSIX path of (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 text
                set newFilePath to theFolder & newFile as text
                set newFilePath2 to theFolder & newFile & "x" as text


                -- copy mail message to the folder and prepend date-time to file name
                set messageId to id of msg
                set myFolder to POSIX path of (account directory of account of mailbox of msg as text)
                do shell script "find " & quoted form of myFolder & " \\( -name \"" & messageId & ".eml\" -a -exec cp -a {} " & quoted form of newFilePath & " \\; \\) -o \\( -name \"" & messageId & ".emlx\" -a -exec cp -a {} " & quoted form of newFilePath2 & " \\; \\)"

            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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.