On May 27, 2009, at 8:32 AM, Reza Farhad wrote:
Can anybody help me with the script in Mail to save a message as source.
I would like to use the AppleScript command "save ___ in ___ as ___ " to save a message into particular location.
I tried to get Mail's "save ___ in ___ as ___ " command to work, but it seems not to want to cooperate. If you simply want a copy of the message as an .emlx file then something like this will work ...
tell application "Mail"
activate
set frontViewer to message viewer 1
set selected mailboxes of frontViewer to {trash mailbox}
set msg to first message of frontViewer
set selected messages of frontViewer to {msg} -- for example
--
-- I tried to get Mail's 'save' command to work here
-- but I just couldn't make it happen
--
set msgID to (id of msg) as text
end tell
set mailFolder to (((path to library folder from user domain) as text) & "Mail:") as alias
tell application "Finder"
try
set msgAlias to some file of entire contents of mailFolder whose name starts with msgID
reveal msgAlias -- for display only, you don't need this
on error errText number errNr
"Error = " & errNr & return & errText
display dialog the result
end try
-- Now that you have an alias to the file of the message, you can command
-- the Finder to save it wherever you like. The saved file will be an .emlx file.
-- This is equivalent to using Mail's File/Save as menu with Raw Message Source selected.
end tell
'entire contents' may be slow. If this is a problem you can use Mail's 'container of' command to reconstruct the exact path to the folder in /Library/Mail. Hope this helps.