AppleScript绑定邮件规则后无法自动触发执行求助
问题分析与解决
核心代码结构问题
你的tell application "Messages"代码块写在了on perform mail action with messages处理器外面。当Mail应用触发规则时,只会执行on perform mail action with messages内部的代码,外面的iMessage发送逻辑完全不会被执行——这就是手动应用规则没发消息、接收邮件时脚本看似未触发的核心原因。
附加权限排查点
接收邮件时脚本完全没反应,还可能涉及系统权限问题:
- 打开「系统设置」→「隐私与安全性」→「自动化」,确认Mail应用被允许控制Messages应用
- 同样在隐私设置的「剪贴板」选项中,确保Mail和Messages都拥有访问权限
修正后的完整代码
using terms from application "Mail" on perform mail action with messages matchmsgs for rule Alertrule tell application "Mail" set msg to item 1 of matchmsgs set msgcontent to (content of msg) as Unicode text end tell -- 将iMessage发送逻辑移至处理器内部,确保规则触发时执行 tell application "Messages" set targetBuddy to "+12223334444" set targetService to (id of 1st account whose service type = iMessage) set textMessage to "ALERT: " & msgcontent -- 直接使用变量,避免剪贴板干扰 set theBuddy to participant targetBuddy of account id targetService send textMessage to theBuddy end tell end perform mail action with messages end using terms from
额外优化说明
- 移除了剪贴板中转步骤,直接用
msgcontent变量拼接消息,避免其他操作占用剪贴板导致的异常 - 所有业务逻辑都被包裹在规则触发的处理器内部,确保Mail规则执行时完整运行发送流程
修改完成后,重新将脚本绑定到Mail规则,并重启Mail应用确保设置生效。
内容的提问来源于stack exchange,提问作者GuideB0t
相关产品推荐
相关产品推荐

