不适用于Mountain Lion的Applescript路径

时间:2012-09-29 18:58:37

标签: path applescript osx-mountain-lion

这个脚本一直在使用10.7及更早版本,但是在10.8中,它似乎已经坏了。这一行:

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

在以前的版本上工作得很好,但Apple显然阻止它在Mountain Lion上正常工作。有没有其他方法可以通过Mountain Lion中的Apple脚本访问该文件夹?

编辑:我已经包含了脚本的整个代码,它将在邮件规则中将整个邮件导出到我的程序可以导入的文本文件中。文本文件被发送到〜/ Library / Application Support / MyProgram / MailImport /

确保您的计算机上已存在该目录,就像我在此处所做的那样,并且Apple脚本不会对其进行任何检查。

当代码中有path to application support时,此脚本不起作用,但将其更改为path to desktop工作正常,这意味着写入应用程序支持文件夹时出现问题,但代码有效。

要进行测试,您可以在Mail中创建新规则,并让Every Message运行该脚本。您必须将脚本放在〜/ Library / Application Scripts / com.apple.mail /

然后它将在规则窗口中显示为一个选项。您可以右键单击一条消息,然后选择“应用规则”以测试单个消息上的脚本。

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 个答案:

答案 0 :(得分:1)

AppleScript中没有“富文本”这样的东西。它应该只是作为“文本”。另外,theFilePath是一个字符串,所以在下一行你需要像这样引用它...打开访问文件theFilePath。注意“文件”这个词。你需要那个词来将字符串转换为文件引用,这是该命令所需要的。

编辑: 现在我看到了你的整个代码,我会这样写。您的问题仍然可能是沙盒问题,但至少应该消除脚本中任何可能的编码错误来源。这将为您提供获得成功脚本的最佳机会。如果它仍然无效,则可能是沙盒问题。

我看到的基本编码问题是您告诉Mail执行所有命令。 Mail不知道诸如“应用程序支持的路径”,“执行shell脚本”或如何写入文件等命令。它们是Applecript命令,因此您不应该告诉Mail执行它们。它们不在Mail的Applecript字典中,因此当Mail尝试执行它们时可能会感到困惑。这就是你提到的“文本”不断变为“富文本”的原因。

所以试一试。如果您仍有问题,那么至少您知道您已尽力消除代码中的错误来源。

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 :尝试使用此处理程序代替上述代码中的处理程序。这可能是使writeToFile处理程序工作的一种方法,因为写入部分将发生在与applescript不同的进程中。值得一试!

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 :如果edit2不起作用,请查看here。似乎其他人在邮件写入某些位置时遇到问题,并通过向Mail添加密钥来解决该问题。

答案 1 :(得分:0)

因此,事实证明这是一个沙盒问题。 10.8中的Apple Mail通常使用沙盒应用程序支持文件夹位置,无论您尝试获取~/Library/Application Support/有多难,所以从10.8上的邮件中的AppleScript

path to application support from user domain

返回路径

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

从那里可以创建和访问MyApplication:MailImport:个文件夹。由于我们尝试读取输出的实际程序没有沙盒,我们现在只能从该位置读取和访问数据,因为它似乎工作正常。