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

Aug 5, 2012 10:18 PM in response to pfly

I've gotten a little further. Here's the script I'm using, I added a subject to the file name, but otherwise I think it's pretty close to Screaming.Pict's original. Where I'm having problems is on the execution of the rule. Annoying thing is it works if I select apply rule, but it doesn't work if I send a test message to trigger the rule.


using terms from application "Mail"

on perform mail action with messagestheselectedMessagesfor ruletheRule


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


set folder_path to "/Users/pfly/Downloads/Dropbox/Chemistry/" & senderName & "-" & subject of theMessage & "-" & originalName


set file_path to (folder_path)


try



savetheAttachmentinfile_path

end try

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Aug 6, 2012 5:21 AM in response to pfly

pfly- your script looks OK. One thing that I did notice was that finder paths in applescript usually expect ":' instead of "/" in folder names.


I wonder if that might spark the difference between running it manually (where you're not sandboxed) and running it automatically.


Try changing the path to;


set folder_path to "Users:pfly:Downloads:Dropbox:Chemistry:" & senderName & "-" & subject of theMessage & "-" & originalName

and let us know what happens.

Aug 6, 2012 6:29 AM in response to Screaming.Pict

Switched the path to


"Users:pfly:Downloads:Dropbox:Chemistry:"


and it didn't work manually or automatically. I added in my MacHD before the first colon and then it worked, but still only manually. This is what the path is now... "MacHD:Users:pfly:Downloads:Dropbox:Chemistry:"


I'm not sure why I moved to the /. At some point it seemed to help, but I'll leave it as the : since that seems more official.


I did a search on scripts working manually but not automatically and found some clues, but no answers. The frustrating thing is that it seems that the script is ok, but Mail isn't using it automatically. One thing I found was to save the script as a bundle, but that doesn't seem to work anymore.


A test I just ran was to add a sound in the rule before the script and a color change after the script. Both happen automatically, so it doesn't seem like the script is causing the rule to stop working.


Thanks for your interest/help.

Aug 6, 2012 6:43 AM in response to pfly

OK so the next step is to get some diagnostics going.


Put a bunch of lines in to display your variables at each step with 'display dialog'


e.g. before


set file_path to (folder_path)


Put in a line;


display dialog "File path: " & file_path


do the same before & after lots of statements and you should be able to see whether your variables are correct and if the relevant parts of the script are being run.


P.S. you could also try just changing the save folder to your downloads folder in case there's a permissions issue somewhere.

Aug 6, 2012 7:02 AM in response to Screaming.Pict

Tried the diagonstics, great tip. I just did it as shown below to make it a little quicker. It stops at 2, so there seems to be a problem with

set senderName to extract name fromtheMessage's sender.


When I comment out that line, it then stops at 3.


When I manually run the script, it goes all the way up to 7 and successfully finishes the script.


P.S. The script doesn't seem to even get to the save path, but I tried chaning it to just downloads just to see and that didn't work.






using terms from application "Mail"

on perform mail action with messagestheselectedMessagesfor ruletheRule



display dialog "1"



tell application "Mail"

repeat with theMessage in theselectedMessages


-- get the sender's name



display dialog "2"


set senderName to extract name fromtheMessage's sender



display dialog "3"


repeat with theAttachment in theMessage's mail attachments

set originalName to name of theAttachment



display dialog "4"


set folder_path to "MacHD:Users:pmfleisch:Downloads:Dropbox:Chemistry:" & senderName & "-" & subject of theMessage & "-" & originalName




display dialog "5"


set file_path to (folder_path)



display dialog "6"


try


-- Save the attachment


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


-- ** file in the location indicated



display dialog "7"



savetheAttachmentinfile_path

end try

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Aug 6, 2012 7:24 AM in response to pfly

Hmm.. I'm none the wiser from that.


One thing to try- I noticed that you were setting the variablefolder_path using subjectoftheMessage.


Could you try taking that out and see if anything changes- just in case applescript isn't a fan of compounding the statement like that? In the script I use I set that in a variable and then use the variable instead of putting it directly into the statement.

Aug 6, 2012 8:21 AM in response to Screaming.Pict

I took out the subject variable and it didn't make a difference.


What I don't understand is why it works manually but not automatically. Doesn't it seem like the kind of thing that should either work or not work?


Can you resend your exact script so I can try that out? I think all I'd have to change is the location, right?

Aug 6, 2012 8:50 AM in response to pfly

pfly,


Below is my exact script, saved in ~/Library/Application Scripts/com.apple.mail/SaveAttachments.scpt


All that you need to do is change the location. I would suggest initially just taking the '3 month holding folder' off so that it saves to 'Downloads' as that's the location that Mail.app is happiest saving to.


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 (must be somewhere Mail can write to)

set attachmentsFolder to homeFolder & "Downloads: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


savetheAttachmentinsavePath

end try

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Aug 6, 2012 9:21 AM in response to Screaming.Pict

Screaming.Pict,


Just tried it and it didn't work. Did the diagnostic and it's getting stuck on the line below just like mine is. Is there another way to do this part? Is there any reason it would work for you, but not for me? A permission somewhere?


set senderName to extract name fromtheMessage's sender

I did play around and if I take that line out as well as


repeat with theMessage in theselectedMessages


then the next place it gets stuck is


set originalName to name of theAttachment

Aug 6, 2012 1:28 PM in response to Screaming.Pict

did a repair permissions. found a few small things, but not mail or applescript related.


I read this article. It explains a little why I can manually run the script, but it isn't automating. But, I think I've got everything in the right place... script is stored in the com.apple.mail folder of my user's library.


http://www.macworld.com/article/1165641/how_increased_mac_security_measures_will _impact_applescript.html

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.