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

请求调试AppleScript:统计指定邮箱邮件数量并写入文本文件

修复后的AppleScript脚本及问题解析

原脚本存在几个关键问题:

  • 命令作用域错误:tell application "Mail"的作用域错误包含了文件操作命令(文件操作属于AppleScript核心命令,无需放在Mail的tell块内)
  • 邮箱引用模糊:mailbox of account "Google"未指定具体邮箱,Google账户下默认包含收件箱、已发送等多个独立邮箱
  • 数据格式问题:直接写入数字到文件可能引发编码异常,需转换为字符串格式
  • 无异常处理:文件打开失败后可能导致文件长期处于锁定状态

修复后的脚本

-- 获取Google账户指定邮箱的邮件数量
tell application "Mail"
    set googleAccount to account "Google"
    -- 此处指定统计收件箱邮件,可按需改为"Sent Messages"等其他邮箱名称
    set targetMailbox to mailbox "Inbox" of googleAccount
    set messageCount to count of messages of targetMailbox
end tell

-- 将统计结果写入指定文件
set filePath to "/Users/topherlukec/Library/CloudStorage/Dropbox/HAZEL/E-MAILS/Google.txt"
try
    set fileHandle to open for access filePath with write permission
    -- 清空文件原有内容后写入字符串格式的统计值
    set eof fileHandle to 0
    write (messageCount as string) to fileHandle
    close access fileHandle
on error errMsg
    try
        close access filePath
    end try
    display dialog "写入文件失败: " & errMsg
end try

关键修改说明

  • 拆分逻辑块:将邮件统计与文件操作分离,确保命令作用域正确
  • 明确目标邮箱:指定具体邮箱名称,避免模糊引用导致的错误
  • 格式转换:将数字转为字符串后写入,保证文件编码正常
  • 异常防护:通过try/on error块确保文件无论是否出错都能被正确关闭,避免锁定问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:12:34