I have been searching a script to streamline following actions:
I sort my emails in Mail.app in ‘mailboxes’ for each separate project. Every few weeks i save these emails to a server to keep a tidy and low-sized mailbox. What i want is the mails to be named as this: date_sender_subject.emlx with date being the date sent.
After some searching and copy-pasting i came up with the following script:
UPDATED
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, seconds:sec} to (msgDate)
set msgDate to ("" & y & "-" & my pad2(m as integer) & "-" & my pad2(d) & "_" & my pad2(h) & "u" & my pad2(min) & "m" & my pad2(sec))
-- assign subject of msg
set msgSubject to ("_" & subject of msg)
-- assign sender of msg
set msgSender to (extract name from sender of msg)
-- assign receiver of msg
set msgReceiver to name of to recipients of msg
-- create filename.eml to be use as title saved - leave out first two characters as in: 13 instead of 2013
set newFile to (characters 3 through -1 of (msgDate & "_" & msgSender & "_to_" & msgReceiver & msgSubject & ".eml")) as rich text
set newFilePath to theFolder & newFile as rich text
set newFilePath2 to theFolder & newFile & "x" as rich 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 rich text)
tell current application to 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 1
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
The current output is something like:
13-03-13_15u15m30_Mickey Mouse_to_Donald Duck_subject.emlx
With Mickey being the sender, and Donald the receiver of the mail.
The “_” is added before the msgSubject (instead of later in the ‘set newFile’, to solve the problem of emails with no subject.
Current problem:
I get no warnings and it seems to work as it should, but in the ‘responses’ window i get an error number -10004.
When i do 50 emails at once it exports like 45 and leaves out some, between which i can find no pattern. Does anyone have an idea?
Things i would like to add/change to this script are:
-
Solve the above mentioned problem
-
The date-part now includes hours, minutes and seconds to avoid identical filenames. A better solution (but it seems more complicated to me) would be serializing when a duplicate files would appear resulting in:
13-03-13_Mickey Mouse_to_Donald Duck_subject_1.emlx -
Remove all characters that don’t belong in a filename such as: spaces, /><, . @ … etc.
I can imagine some of these actions could be done in a separate renaming program, but i would like to include it in one script.
I am a complete applescript-rookie so forgive me for my ignorance.
Thx
ML