I got Mountain Lion the other day and found that Mail.app still has a bug whereby it can corrupt messages on an Exchange server sometimes when removing attachments.
To that end I'm trying to learn Applescript to do several things;
1- Create a script, run through a rule, to save attachments.
2- Create a script to move a message to a local folder, remove the attachments and then move the message back into my exchange inbox.
I'm afraid I'm falling at the first hurdle. I wrote a script that works as a stand-alone script, but the minute I put in the mail rule handler it ceases to work.
Initially it was failing because 'path to home folder' was returning a sandboxed path (Users/me/Library/Containers/com.apple.mail/Data).
I worked around that with a bit of sed to strip out the extra path. Not sure if that was the right thing to do, but it was 2am...
After doing that the following script seemed to work. However today I am finding that it gets as far as trying to save the file and fails- despute having the correct attachment name and path name.
Can anyone tell me why the save isn't happening? It definitely gets into the 'try' loop and fails at the point it tries to save.
using terms from application "Mail"
on perform mail action with messages theselectedMessages
-- get the home folder
set sandboxedhomeFolder to (path to home folder)
-- strip out the stupid sandboxed rubbish in the folder name
set homeFolder to (do shell script "echo " & sandboxedhomeFolder & " | sed 's/Librar.*//'")
-- add on the desired attachments folder
set attachmentsFolder to homeFolder & "Documents:3 Month Holding Folder"
tell application "Mail"
repeat with theMessage in theselectedMessages
-- get the sender's name
set senderName to extract name from theMessage's sender
repeat with theAttachment in theMessage's mail attachments
set originalName to name of theAttachment
-- name the file with the sender's name as well as the attachment name
set savePath to attachmentsFolder & ":" & senderName & "-" & originalName
-- Save the attachment
-- ** the code seems to get this far and then doesn't actually save the
-- ** file in the location indicated
save theAttachment in savePath
end perform mail action with messages
end using terms from