Creating "save as" functionality to .eml file in AppleScript
5,388
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
Related videos on Youtube
Author by
unieater
Updated on September 18, 2022Comments
-
unieater almost 2 years
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
-
Admin over 11 yearsIs 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? -
Admin over 11 yearsFunctionality 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.
-
Admin over 11 yearsIt might be easier using the existing Save as… and adding some batch renaming afterwards. Would that also be an acceptable solution?
-
Admin over 11 yearsabsolutely acceptable
-
Admin over 11 yearsOn my machine, save as will only save the first of several selected messages.
-
Admin over 11 yearsI've noticed the same, the Save As solution from Mail doesn't give you a batch .eml save capability.
-