I want to export selected emails from a given mailbox to Finder. I found this script on the internet and altered it to my likes. But it doen't seems to export. Does somebody know where it is going wrong?
tell application "Mail"
set msgs to message of mailbox "test1" of account "Info"
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 up month parsing value for French Vanilla algorithm
set fixedDate to current date --or any other date
set month of fixedDate to January
-- set theFolder to "Macintosh HD:Users:rajsingh:Desktop:" as alias
set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles
repeat with msg in msgs
-- get path to message
set mb to mailbox of msg
set mba to account of mb
-- mtype is returning 'constant **** ETIM' when it should be imap (for OGC account)
-- set mtype to (account type) of mba
set mtype to "imap"
set accountpath to account directory of account of mb
set fullpath to accountpath & name of mb & "." & mtype & "mbox:Messages:"
-- figure out message name
set msgfilename to id of msg
set atts to number of mail attachments of msg
if atts > 0 then
set msgfilename to msgfilename & ".partial.emlx"
else
set msgfilename to msgfilename & ".emlx"
end if
set fullpath to fullpath & msgfilename
set theFile to fullpath as rich text
-- create new name prefix based on date
set msgDate to date received of msg
-- parse date
-- use French Vanilla algorithm to get month number
set theMonth to (2 + (msgDate - fixedDate + 1314864) div 2629728) as rich text
if length of theMonth < 2 then
set theMonth to "0" & theMonth
end if
set theDay to day of msgDate as rich text
if length of theDay < 2 then
set theDay to "0" & theDay
end if
set msgDate to (year of msgDate as rich text) & theMonth & theDay & "at" & hours of msgDate & minutes of msgDate & seconds of msgDate
set comparison_string to ":/"
set replacement_string to "->"
set msgSubject to ""
set msgSubject to my replaceText("Re- ", "", msgSubject)
set msgSubject to my replaceText("Re-", "", msgSubject)
--set msgSubject to text 1 thru 49 of msgSubject
set newFile to msgSubject & "_" & msgDate & ".emlx" as rich text
--set newFile to (msgSubject & "_" & msgDate & "_" & msgfilename) as text
-- copy mail message to the folder and prepend date-time to file name
tell application "Finder"
try
set intFile to ""
duplicate file theFile to folder theFolder
on error
display dialog theFile & theFolder
display dialog "couldn't duplicate " & intFile
end try
-- rename file
try
set name of intFile to newFile
on error
display dialog "couldn't set name of " & intFile & " to " & newFile
end try
-- reveal intFile
-- open theFile as alias
end tell
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 replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText