0

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

2 回答 2

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 不起作用,请看这里。似乎其他人在将邮件写入某些位置时遇到了问题,并通过向邮件添加密钥以授予其权限来解决该问题。

于 2012-09-29T20:14:46.400 回答
0

所以它原来是一个沙盒问题。10.8 中的 Apple Mail 通常使用 Sandboxed Application Support 文件夹位置,无论您如何努力获取~/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:可以创建和访问文件夹。由于我们尝试读取输出的实际程序没有沙盒化,我们现在可以从该位置读取和访问数据,因为它似乎工作正常。

于 2012-10-01T20:10:06.067 回答