Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to save mail attachments failing in Mountain Lion

Hi,


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 tohome 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 messagestheselectedMessages

-- get the home folder

set sandboxedhomeFolder to (path tohome 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 fromtheMessage'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

try

-- Save the attachment

-- ** the code seems to get this far and then doesn't actually save the

-- ** file in the location indicated

savetheAttachmentinsavePath

end try

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Posted on Jul 27, 2012 9:42 AM

Reply
52 replies

Dec 23, 2013 8:38 AM in response to Screaming.Pict

Here's a script I found that works under Mavericks - need to work on it to make it work as a rule.


tell application "Mail"

set theMessages to selection

set theMessage to item 1 of theMessages

set theAttachments to every mail attachment of theMessage

repeat with a from 1 to length of theAttachments

set theAttachment to itema of theAttachments

set theOutputFolder to (choose folder) as string

try

set theAttachmentName to name of theAttachment

set theSavePath to theOutputFolder & theAttachmentName


savetheAttachmentintheSavePath

end try

end repeat

end tell



tell application "Finder"


revealtheSavePath

set bounds of Finder window 1 to {10, 50, 1000, 500}


activate

end tell

Dec 23, 2013 9:19 AM in response to rich graham1

I used this script in the mail.app as a rule and it doesn’t work, theres no error but there is no activity…

Is there some error in my script?


Thanks.



tell application "Mail"


set theMessages to selection

set theMessage to item 1 of theMessages

set theAttachments to every mail attachment of theMessage

repeat with a from 1 to length of theAttachments

set theAttachment to itema of theAttachments

set theOutputFolder to (attachments) as string

try

set theAttachmentName to name of theAttachment

set theSavePath to theOutputFolder & theAttachmentName


savetheAttachmentintheSavePath

end try

end repeat

end tell





tell application "Finder"


revealtheSavePath

set bounds of Finder window 1 to {10, 50, 1000, 500}


activate

end tell

Applescript to save mail attachments failing in Mountain Lion

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.