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

AppleScript保存邮件附件异常求助:大附件存为空文件

解决macOS High Sierra下AppleScript保存大邮件附件为空的问题

嘿,我之前在High Sierra上用AppleScript处理邮件附件时也碰到过一模一样的问题!小文件秒存没问题,稍大一点的就出空文件,折腾了好久才找到几个靠谱的解决思路,给你参考下:

1. 核心原因:附件还没加载完就执行了保存

High Sierra的Mail app默认不会自动预加载所有大附件,你的脚本可能在附件还没完全下载到本地时就触发了保存操作,自然就生成空文件了。给脚本加个“等待附件下载完成”的逻辑就行:

tell application "Mail"
    set selectedMessages to selection
    repeat with msg in selectedMessages
        set msgAttachments to attachments of msg
        repeat with att in msgAttachments
            -- 循环等待,直到附件下载完成
            repeat while downloaded of att is false
                delay 1 -- 每隔1秒检查一次
            end repeat
            -- 指定保存路径(这里用下载文件夹为例)
            set saveLocation to (path to downloads folder as text) & name of att
            save att in file saveLocation
        end repeat
    end repeat
end tell

2. 换一种更稳定的文件写入方式

如果上面的等待逻辑还是不行,可能是Mail的save命令对大文件支持有问题,试试直接读取附件数据再写入文件,绕开Mail的保存机制:

tell application "Mail"
    set selectedMessages to selection
    repeat with msg in selectedMessages
        set msgAttachments to attachments of msg
        repeat with att in msgAttachments
            repeat while downloaded of att is false
                delay 1
            end repeat
            -- 提取附件的二进制数据
            set attachmentData to data of att
            set savePath to (path to downloads folder as text) & name of att
            -- 用Finder直接创建文件并写入数据
            tell application "Finder"
                set newFile to make new file at folder (path to downloads folder) with properties {name:name of att}
                open for access newFile with write permission
                write attachmentData to newFile
                close access newFile
            end tell
        end repeat
    end repeat
end tell

3. 检查权限与缓存问题

  • 权限确认:确保你保存附件的文件夹(比如Downloads)有写入权限,High Sierra的系统权限管控比较严格,尽量用用户自己的个人文件夹,别写到系统目录里。
  • 清理Mail缓存:有时候Mail的缓存文件损坏会导致附件加载异常,你可以关闭Mail后,删除~/Library/Caches/com.apple.mail目录下的缓存文件(删除前记得备份),然后重启Mail再试脚本。

你可以先从第一种方法试起,这是最常见的解决方式,大概率能搞定你的问题!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:17:44