Applescript 无法在 Mountain Lion 中工作的路径

Posted

技术标签:

【中文标题】Applescript 无法在 Mountain Lion 中工作的路径【英文标题】:Applescript path to not working in Mountain Lion 【发布时间】:2012-09-21 06:09:24 【问题描述】:

此脚本一直在 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

【问题讨论】:

这很可能与权利/沙盒乐趣有关,但希望有一些方法可以解决这个问题,以实现与以前系统相同的功能。 【参考方案1】:

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

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

我看到的基本编码问题是您告诉 Mail 执行所有命令。 Mail 不知道诸如“应用程序支持路径”、“执行 shell 脚本”之类的命令,也不知道如何写入文件。它们是 applescript 命令,因此您不应让 Mail 执行它们。它们不在 Mail 的 applescript 字典中,因此当 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。似乎其他人在将邮件写入某些位置时遇到了问题,并通过向邮件添加密钥以授予其权限来解决该问题。

【讨论】:

在脚本编辑器中保存时,会自动将path to application support from user domain as text替换为path to application support from user domain rule type rich text 另外作为一个单独的说明,将原始代码中的path to application support 更改为path to desktop 会导致脚本正常执行,因此它必须是权限问题而不是语法问题。 第三条评论:我已经包含了整个脚本。当简单地将“应用程序支持路径”替换为“桌面路径”时,此脚本可以正常编译和运行 我了解您的 3 cmets。我在答案中添加了“编辑”部分。试试看。 感谢您的跟进。代码仍然像以前一样工作,只有在写入桌面时才能正常工作,而在写入应用程序支持时不能正常工作。看来肯定是和沙盒直接相关的。有没有办法通过 AppleScript 使用通过 Apple Mail 支持应用程序的路径?【参考方案2】:

所以它原来是一个沙盒问题。 10.8 中的 Apple Mail 通常使用沙盒应用程序支持文件夹位置,无论您多么努力地尝试获取 ~/Library/Application Support/,因此来自 10.8 上 Mail 中的 AppleScript

path to application support from user domain

返回路径

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

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

【讨论】:

以上是关于Applescript 无法在 Mountain Lion 中工作的路径的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Mountain Lion 上安装 therubyracer -v '0.10.0'

Xcode 13 AppleScript 框架无法将 UI 连接到 Applescript 代码

无法在“邮件”应用程序中执行 AppleScript

在 OSX 更新到优胜美地后,Applescript 无法“告诉”Spotify 播放

蒙特雷更新后python中的applescript无法正常工作

升级到 Mountain Lion 后无法访问 MySQL 数据库(使用 MAMP)