AppleScript 在 Mac Mail.app 中处理传入的电子邮件

AppleScript to process incoming emails in Mac Mail.app

其实我的问题和this one一样,答案已经让我很进步了。 (总结:我希望我的 python 脚本 运行 出现在每封具有特定主题的电子邮件上,并从内容中提取数据。)

无论如何,我是 AppleScript 的新手,找不到将触发电子邮件的内容作为参数提供给我的 python 脚本的解决方案。

而实际上我只需要传递邮件的html内容即可。

任何人都可以指出我正确的方向或为黑暗带来光明吗?非常感谢。

我的 AppleScript 目前看起来像这样,虽然我的脚本现在应该打印电子邮件内容,但什么也没有发生:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"
            repeat with eachMessage in theMessages
                set theContent to source of eachMessage
                do shell script "python completePathToPyScript.py" & theContent
            end repeat
        end tell
    end perform mail action with messages
end using terms from

然后我的 python 脚本执行以下测试:

import sys

email_data = str(sys.argv[1])
print email_data

还是我接数据的方式不对?

试试这个。请注意,您不应使用 tell application "Mail",因为此事件是由 Mail 触发的,因此您可以访问 Mail 提供的所有功能。在这种情况下,using terms from application "Mail" 就是您所需要的。您可以通过键入 command+shift+o 并选择 "Mail.app" 来查找更多信息.这将向您显示 Mail 的字典,并且应该是进一步代码开发的良好起点。我现在修改它以在没有任何特定规则的情况下工作,作为我过去遇到的一些问题的解决方法:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            if subject of eachMessage contains "Express222" then
                set theContent to source of eachMessage
                display alert theContent
            end if
        end repeat
    end perform mail action with messages
end using terms from