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

如何用AppleScript获取邮件消息属性?代码报错求助

AppleScript 获取邮件属性及删除操作的正确实现

问题分析

你尝试的两种方案都存在语法或作用域问题,以下是修正后的实现方式,同时支持后续删除邮件的操作:

方案一(批量获取前10封邮件属性)

修正后的代码:

tell application "Mail"
    set counter to 1
    repeat with each_message in messages of mailbox "INBOX" of account "iCloud"
        -- 直接获取邮件属性,无需额外转reference
        set receivedDate to date string of date received of each_message
        log receivedDate -- 可替换为你需要的处理逻辑
        
        -- 如需删除邮件,取消下面一行注释
        -- delete each_message
        
        set counter to counter + 1
        if counter > 10 then exit repeat
    end repeat
end tell

原方案一的错误点:

  • 未将操作包裹在tell application "Mail"块中,AppleScript无法识别Mail应用的对象模型
  • each_message本身就是Mail的message对象,不需要再用a reference to转换
  • 计数器赋值语法错误:AppleScript中变量赋值用set counter to counter + 1,而非counter = counter + 1
  • 循环终止需用exit repeat而非stop

方案二(获取单封邮件属性)

修正后的代码:

tell application "Mail"
    set the_message to last message of mailbox "INBOX" of account "iCloud"
    open the_message
    set receivedDate to date string of date received of the_message
end tell
-- display dialog属于系统标准命令,放在Mail的tell块外执行
display dialog receivedDate

原方案二的错误点:

  • display dialog是Standard Additions的命令,放在tell application "Mail"块内时,Mail应用无法识别该命令,需移到块外执行
  • 无需额外添加a reference to,直接获取message对象即可正常访问属性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 10:22:14