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

求助:如何通过Apple Mail规则自动保存邮件中PDF链接的文件?

我明白你现在的困扰——手动用Automator服务能搞定,但想让Apple Mail规则自动执行就卡壳了对吧?我之前也碰到过类似情况,下面给你一套亲测有效的方案,结合AppleScript和Mail规则来实现自动保存PDF链接的功能:

解决方案:Apple Mail规则 + AppleScript自动保存PDF链接

第一步:编写适配Mail规则的AppleScript

你之前的AppleScript大概率没适配Mail规则的输入逻辑(规则会把触发的邮件作为参数传递给脚本),下面这个脚本专门针对Mail规则设计,能提取邮件中的PDF链接并自动下载到指定文件夹:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        -- 替换成你自己的目标文件夹路径
        set targetFolder to POSIX file "/Users/你的用户名/Documents/自动保存PDF" as alias
        
        repeat with eachMessage in theMessages
            -- 获取邮件内容并提取所有链接
            set messageContent to content of eachMessage
            set linkList to extract links from messageContent
            
            repeat with eachLink in linkList
                -- 筛选出PDF格式的链接
                if eachLink ends with ".pdf" then
                    -- 用curl后台下载(无需打开浏览器)
                    set AppleScript's text item delimiters to "/"
                    set linkParts to text items of eachLink
                    set fileName to last item of linkParts
                    set AppleScript's text item delimiters to ""
                    
                    -- 组装curl下载命令
                    set downloadPath to POSIX path of targetFolder & "/" & fileName
                    set downloadCommand to "curl -L -o " & quoted form of downloadPath & " " & quoted form of eachLink
                    do shell script downloadCommand
                end if
            end repeat
        end repeat
    end perform mail action with messages
end using terms from

-- 辅助函数:从邮件文本中提取所有HTTP/HTTPS链接
on extract links from inputText
    set linkRegex to "\\bhttps?://\\S+\\b"
    set AppleScript's text item delimiters to "\n"
    set textLines to text items of inputText
    set AppleScript's text item delimiters to ""
    set foundLinks to {}
    
    repeat with eachLine in textLines
        set currentOffset to 1
        repeat
            set matchResult to find text linkRegex in eachLine starting at currentOffset with regex and all occurrences
            if matchResult is {} then exit repeat
            set end of foundLinks to matchResult's item 1's match text
            set currentOffset to matchResult's item 1's match range's end + 1
        end repeat
    end repeat
    return foundLinks
end extract links from

脚本注意事项:

  • 一定要把targetFolder里的路径替换成你实际要保存PDF的文件夹路径(比如/Users/John/Downloads/自动归档PDF)
  • 用curl下载的好处是完全后台执行,不会弹出浏览器,比模拟快捷键更稳定;-L参数是为了处理跳转链接

第二步:在Mail中创建规则并关联脚本

  1. 打开Apple Mail,点击顶部菜单栏的Mail > 设置 > 规则
  2. 点击添加规则,设置你的触发条件(比如「主题包含PDF」「发件人是指定邮箱」,完全根据你的需求来)
  3. 在「执行以下操作」的下拉菜单里,选择运行AppleScript,然后点击选择脚本,找到你刚才保存的.scpt文件
  4. 点击确定保存规则,之后可以发送一封带PDF链接的测试邮件给自己,验证是否自动保存到目标文件夹

常见问题排查

  • 权限不足:如果脚本无法执行,去系统设置 > 隐私与安全性 > 辅助功能里,把Mail、Script Editor(你写脚本的工具)都添加进去,授予控制权限
  • 链接提取不全:如果有些PDF链接藏在HTML邮件的富文本里,你可以把邮件默认显示改成纯文本(Mail设置 > 查看 > 显示为纯文本),这样链接会直接暴露在文本里,更容易被提取
  • 下载失败:如果遇到跳转链接或需要验证的链接,可以给curl命令加-k参数(忽略SSL验证),或者延长超时时间--connect-timeout 10

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:57:18