You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mail.app规则触发AppleScript时传入错误消息,如何获取正确消息?

解决Mail.app规则自动触发时AppleScript获取错误邮件的问题

我在High Sierra的Mail 11.2上也遇到过一模一样的问题——手动右键应用规则时脚本完全正常,但新邮件自动触发规则时,theMessages里总会混入完全不相关的随机邮件。这其实是这个版本Mail规则引擎的一个小bug,根源在于自动处理时的邮件集合缓存机制出了问题。下面是几个亲测有效的解决办法:

方法1:在脚本内手动过滤符合规则条件的邮件

既然规则已经触发,但传入的邮件集合有杂质,我们可以在脚本里再加一层校验,只处理真正符合规则逻辑的邮件。比如你的规则是“主题包含「紧急」”,就可以在循环里加判断:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with msg in theMessages
			-- 替换成你规则的实际判断条件,比如检查主题关键词
			if subject of msg contains "紧急" then
				set theText to subject of msg & return & content of msg & date sent of msg
				display dialog (theText)
			end if
		end repeat
	end perform mail action with messages
end using terms from

这样即使theMessages里混进了无关邮件,也会被直接过滤掉,只处理符合要求的内容。

方法2:直接从目标邮箱获取最新邮件

如果你的规则会把符合条件的邮件移动到指定邮箱(比如「规则处理箱」),那可以直接跳过theMessages,从目标邮箱里精准获取最近收到的邮件:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		-- 替换成你的目标邮箱名称和对应的账户名
		set targetMailbox to mailbox "规则处理箱" of account "你的iCloud账户"
		-- 只处理最近1分钟内收到的邮件,避免误操作旧邮件
		set validMessages to every message of targetMailbox whose date received is greater than (current date) - 1 * minutes
		
		repeat with msg in validMessages
			set theText to subject of msg & return & content of msg & date sent of msg
			display dialog (theText)
		end repeat
	end perform mail action with messages
end using terms from

这种方法彻底绕开了theMessages的bug,直接获取最准确的目标邮件集合。

方法3:重建Mail的规则数据库

有时候问题是因为Mail的规则缓存损坏导致的,重建规则就能解决:

  • 打开Mail偏好设置(快捷键Cmd+,),切换到「规则」标签页
  • 右键点击规则列表,选择「导出规则」,把所有规则备份到本地
  • 删除列表里的所有规则,然后重启Mail.app
  • 重启完成后再导入刚才备份的规则,重新测试自动触发场景

至于为什么手动应用规则时正常?因为手动操作时,Mail是直接把你选中的邮件集合传入theMessages,没有经过自动触发时的缓存批量处理逻辑,所以不会出现随机邮件的问题。

内容的提问来源于stack exchange,提问作者ghenne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:50:02