質問

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
役に立ちましたか?

解決 2

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.8

path to application support from user domain

Returns the path

~/Library/Containers/com.apple.mail/Data/Library/Application Support/

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.

他のヒント

There's no such thing in applescript as "rich text". It should only be as "text". In addition theFilePath is a string, so in the next line you need to reference it like this... open for access file theFilePath. Notice the word "file". You need that word there to turn the string into a file reference which is what that command requires.

EDIT: Now that I see your entire code I would write it like this. Your problem still may be a sandboxing issue but at the very least you should eliminate any sources of possible coding errors in your script. This will give you the best chance of having a successful script. If it still doesn't work then it probably is a sandboxing issue.

The basic coding issues that I see are that you are telling Mail to perform all of the commands. Mail doesn't know commands such as "path to application support", "do shell script", or how to write to a file. They're applescript commands so you shouldn't tell Mail to perform them. They are not in Mail's applescript dictionary and thus they may be getting confused when Mail tries to perform them. That's certainly the reason that "text" keeps changing to "rich text" as you mention.

So give this a try. If you still have the problem then at least you know that you've done all you can to eliminate sources of errors in your code.

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>"
                my writeToFile(TotalString)
            end repeat
        end tell
    end perform mail action with messages
end using terms from

on writeToFile(TotalString)
    set UniqueName to do shell script "uuidgen"
    set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName
    set theFileReference to open for access file theFilePath with write permission
    write TotalString to theFileReference
    close access theFileReference
end writeToFile

EDIT2: try this handler in place of the one in the above code. This may be one way to make the writeToFile handler work because the writing part would happen in a separate process from the applescript. It's worth a try!

on writeToFile(TotalString)
    set UniqueName to do shell script "uuidgen"
    set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName
    set theResult to do shell script "echo " & quoted form of TotalString & " > " & quoted form of POSIX path of theFilePath
end writeToFile

EDIT3: if edit2 doesn't work then look here. It seems others have had problems with Mail writing to certain locations and solved it by adding a key to Mail to give it permission.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top