Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Jul 30, 2012 9:32 AM in response to Screaming.Pict

Hey Pict,


This may be a few days too late, but I recently upgraded to Mountain Lion and found that my old applescripts that automatically download mail attachments don't work anymore. I would highly suggest putting a dialog in that last try/catch block, it might give you some more info. Here's my own script for reference:


-----------------------------------------------------------

using terms from application "Mail"

on perform mail action with messagesmatching_messages

set report_type to "throughput"

set folder_path to "Macintosh HD:Users:rguerra:code:Rice_WiFi_Data:automatic_reports:" & report_type

set file_path to (folder_path & ":")

tell application "Mail"

set the num_messages to the count of matching_messages


--display dialog "0. Found " & num_messages & " messages!"

repeat with i from 1 to the num_messages

set this_message to (itemi of matching_messages)

tell this_message


--display dialog "1. Processing \"" & (subject of this_message) & "\" which has " & (count of mail attachments of this_message) & " attachment(s)."

repeat with z from 1 to the (count of mail attachments of this_message)

set theAttachment to itemz of (mail attachments of this_message)


--display dialog "2. Got attachment " & (name of theAttachment)

with timeout of 10 seconds

set myname to do shell script "whoami"

display dialog "3.Trying to save " & (name of theAttachment) & " to the path " & (file_path & (name of theAttachment) as string) & " as user " & myname

try


savetheAttachmentin (file_path & name of theAttachment)

on error number errNum

display dialog "ERROR: problem saving file, ernum: " & errNum

error number -128 --User Ended error; this should stop script execution

end try

end timeout

end repeat

end tell

end repeat

end tell

end perform mail action with messages

end using terms from

-----------------------------------------------------------


Anyway, when this automatically triggers in Mail, the other dialogs execute just fine with the expected results. The final error dialog informs me that the attachment save failed with an error code -10000.


I've been researching this problem and it appears to be something with the sandboxd that other people have run into but there isn't a good solution for it yet. The following is the output of my system.log as shown in the console.app (just type "console" into Spotlight to launch the application).


Jul 30 11:24:21 Ryans-MacBook-Air.local Mail[13362]: An exception was thrown during execution of an NSScriptCommand...

Jul 30 11:24:21 Ryans-MacBook-Air kernel[0]: Sandbox: sandboxd(13571) deny mach-lookup com.apple.coresymbolicationd

Jul 30 11:24:23 Ryans-MacBook-Air.local sandboxd[13571] ([13362]): Mail(13362) deny file-write-create /Users/rguerra/code/Rice_WiFi_Data/automatic_reports/throughput/Client_Traffic_ 20120730_094501_442.csv


Clearly, there is a problem with the file permissions for the Mail application. I'm still trying to work out the rules for sandboxing since I don't know precisely what I'm allowed to do.


Does anyone have any more information about where you are allowed to write files from a sandboxed application in OS X 10.8 (Mountain Lion)? More specifically, how one can script such a task?

Jul 30, 2012 9:45 AM in response to Matrixesque

A brief update... I found a good discussion of the new sandbox restrictions here:


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


http://developer.apple.com/library/ios/#DOCUMENTATION/Miscellaneous/Reference/En titlementKeyReference/Chapters/AboutEntitlements.html


That explains why I had to place my old scripts in a new location (I honestly thought this was a Mail idiosyncracy instead of a new OS idiosyncracy). It also explains why our save paths are not allowed--the Mail application has not been "entitled" to save files where we want to place them.


For my application, I don't care where the files end up--I can always make an alias to that location. I just need them to be downloaded automatically. The next step will be to see what Mail is actually entitled to do. :/

Jul 30, 2012 9:56 AM in response to Matrixesque

Matrix,


Thanks for your replies- I thought I had checked my console but I didn't see any errors in there (it was rather late at night and I was falling asleep...).


I agree with you that it seems that when I run the script myself Mac OS lets me go right ahead and do it, but when it is run by Mail.app (through a rule) then it gets sandboxed. This is a bit of a surprise to me as I didn't think that mail.app was sandboxed.


If we can figure out what folder the script *is* allow to save in then I'll happily save the files there (and let Hazel take care of the rest 🙂 )

Jul 30, 2012 10:05 AM in response to Screaming.Pict

Well, I'm halfway there. The following is an XML list of the "entitlements" that Mail.app has. You can see that even Apple has their "hack" with temporary entitlements in there. Nice, Apple.


Now I just gotta figure out what on earth any of these actually DO.


Ryans-MacBook-Air:~ rguerra$ codesign -d --entitlements - /Applications/Mail.app/

Executable=/Applications/Mail.app/Contents/MacOS/Mail

??qq ?<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>com.apple.developer.ubiquity-container-identifiers</key>

<array>

<string>com.apple.mail</string>

</array>

<key>com.apple.developer.ubiquity-kvstore-identifier</key>

<string>com.apple.mail</string>

<key>com.apple.private.ubiquity-additional-kvstore-identifiers</key>

<array>

<string>com.apple.mail.recents</string>

<string>com.apple.mail.vipsenders</string>

</array>

<key>com.apple.private.dark-wake-push</key>

<true/>

<key>com.apple.private.dark-wake-network-reachability</key>

<true/>

<key>com.apple.private.aps-connection-initiate</key>

<true/>

<key>com.apple.security.app-sandbox</key>

<true/>

<key>com.apple.security.files.user-selected.read-write</key>

<true/>

<key>com.apple.security.print</key>

<true/>

<key>com.apple.security.network.client</key>

<true/>

<key>com.apple.security.personal-information.addressbook</key>

<true/>

<key>com.apple.security.personal-information.calendars</key>

<true/>

<key>com.apple.security.files.downloads.read-write</key>

<true/>

<key>com.apple.security.temporary-exception.mach-lookup.global-name</key>

<array>

<string>com.apple.imagent.desktop.Launched</string>

<string>com.apple.imagent.desktop.auth</string>

<string>com.apple.syncservices.SyncServer</string>

<string>com.apple.pubsub.ipc</string>

<string>com.apple.SystemConfiguration.PPPController</string>

<string>com.apple.icbaccountsd</string>

<string>com.apple.mtmd.xpc</string>

<string>com.apple.accountsd.accountmanager</string>

<string>com.apple.familycontrols</string>

</array>

<key>com.apple.security.temporary-exception.sbpl</key>

<string>(allow mach-lookup (global-name-regex #&quot;^[0-9]+$&quot;))</string>

<key>com.apple.security.temporary-exception.files.absolute-path.read-only</key>

<array>

<string>/Library/Images/People/</string>

<string>/AppleInternal/Library/Mail/</string>

<string>/Network/Library/Mail/</string>

<string>/Network/Library/Images/People/</string>

</array>

<key>com.apple.security.temporary-exception.files.home-relative-path.read-write </key>

<array>

<string>/Library/Mail/</string>

<string>/Library/Application Support/iCloud/Accounts/</string>

<string>/Library/Application Support/SyncServices/</string>

</array>

<key>com.apple.private.tcc.allow</key>

<array>

<string>kTCCServiceAddressBook</string>

</array>

</dict>

</plist>

Jul 30, 2012 10:23 AM in response to Matrixesque

Solved!


Apparently the line with:


<true/>

<key>com.apple.security.files.downloads.read-write</key>

<true/>


...allows Mail.app to read/write to your Downloads folder! I tried it out and it's all working now. For reference, my save path is now the following:


set report_type to "throughput"

set folder_path to "Macintosh HD:Users:rguerra:Downloads:automatic_reports:" & report_type

set file_path to (folder_path & ":")


Thanks for the original email! It definitely got me thinking along this line. Whew.


For the record, a list of all the various entitlements and what they mean can be found here:


http://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/Ent itlementKeyReference/EntitlementKeyReference.pdf

Jul 30, 2012 8:27 PM in response to Screaming.Pict

OK, I seem to missing something here. I've got the same problem as you've described but it does not appear as though my script is even running anymore. I changed my script (located in com.apple.mail) to save to the Downloads folder instead of the folder I had been using and it still didn't work. I then threw a Display Dialog at the beginning of the script and the dialog doesn't even pop up. Any ideas?

Jul 31, 2012 5:51 PM in response to jbublik

I don't think you can just add entitlements on the fly, as it were. If you were able to, then applications could just overwrite their entitlements and do anything they wanted. I think those abilities are set when the application is compiled and submitted to the App Store.


The only reason why we were able to do anything was because Mail.app already had the required entitlement to read/write to the ~/Downloads folder, we just didn't know it at the start.

Jul 31, 2012 7:08 PM in response to Matrixesque

I see, so the only difference with the old (snow leopard) version is the


--set attachmentsFolder to ((path to home folder as text) & "Documents:Symbolic") as text was my old one

set attachmentsFolder to "Macintosh HD:Users:jbublik:Downloads:Symbolic:" is my new one.

I tried - it still doesn't work. Why my scripts won't work? I am really puzzled

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.