This script has been working on 10.7 and older, but in 10.8, it seems it’s broken. The line:
set theFilePath to ((path to application support from user domain) as rich text) & "AppFolderName:" & UniqueName as string
set theFileReference to open for access theFilePath with write permission
Worked fine on previous versions, but Apple apparently is preventing it from working properly on Mountain Lion. Is there any other way of getting access to that folder via Apple script in Mountain Lion?
Edit: I’ve included the entire code of the script that will, within a Mail rule export the entire message to a text file that my program can import. The text file is sent to ~/Library/Application Support/MyProgram/MailImport/
Make sure the directory already exists on your machine, as it does here on mine, and the Apple Script doesn’t do any checking for it.
This script does not work when path to application support is in the code, but changing it to path to desktop work fine, meaning there is an issue writing to the application support folder, but the code works.
To test, you can create a new rule in Mail, and have Every Message run the script. You have to put the script in ~/Library/Application Scripts/com.apple.mail/
It will then appear as an option in the rules window. You can right-click a message and select Apply Rules to test the script on an individual message.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set sub to subject of eachMessage
set mid to message id of eachMessage
set sen to sender of eachMessage
set recp to ""
repeat with thisRecpt in recipients of eachMessage
set recp to recp & address of thisRecpt & ","
end repeat
set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage)
set dat to (y * 10000 + m * 100 + d) as string
set tim to (hh * 100 + mm) as string
set con to content of eachMessage
set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>"
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as rich text) & "MyApplication:MailImport:" & UniqueName as string
set theFileReference to open for access theFilePath with write permission
write TotalString to theFileReference
close access theFileReference
end repeat
end tell
end perform mail action with messages
end using terms from
So it turns out to be a Sandboxing issue. Apple Mail in 10.8 uses a Sandboxed Application Support folder location in general regardless of how hard you try to just get
~/Library/Application Support/, so from an AppleScript within Mail on 10.8Returns the path
From there the
MyApplication:MailImport:folders can be created and accessed. Since our actual program that’s trying to read the output isn’t sandboxed we can just read and access the data from that location for now, as it seems to be working fine.