如何通过AppleScript实现Apple Mail邮件PDF附件指定目录自动保存?
Apple Mail规则调用AppleScript保存PDF附件实现方案
一、创建Mail规则步骤
- 打开Apple Mail,依次点击菜单栏「邮件」→「偏好设置」→「规则」
- 点击「添加规则」,设置触发规则的条件(如特定发件人、主题关键词等)
- 在「执行以下操作」下拉列表中选择「运行AppleScript」,点击「新建脚本」
二、现成AppleScript脚本
将以下代码粘贴到新建的脚本编辑器窗口中,修改目标文件夹路径后保存:
using terms from application "Mail" on perform mail action with messages theMessages for rule theRule -- 替换为你的目标文件夹路径,确保文件夹已存在 set saveFolder to POSIX file "/Users/你的用户名/Documents/邮件PDF附件" as alias repeat with eachMessage in theMessages set theAttachments to every attachment of eachMessage repeat with eachAttachment in theAttachments set attachmentName to name of eachAttachment -- 过滤PDF格式附件(区分大小写) if attachmentName ends with ".pdf" or attachmentName ends with ".PDF" then try -- 覆盖同名文件,如需保留重复文件可移除with replacing参数 save eachAttachment in saveFolder with replacing on error errMsg display dialog "保存附件失败: " & errMsg buttons {"确定"} default button 1 end try end if end repeat end repeat end perform mail action with messages end using terms from
三、配置注意事项
- 替换脚本中的
/Users/你的用户名/Documents/邮件PDF附件为实际保存路径,路径需为POSIX格式(macOS标准路径) - 首次触发规则时,系统会弹出权限请求,需允许Mail访问目标文件夹
- 若需避免覆盖同名文件,可修改脚本添加时间戳,示例:
-- 在保存前添加时间戳 set currentDate to current date set dateString to (year of currentDate as string) & "-" & (month of currentDate as integer as string) & "-" & (day of currentDate as string) & "_" & (hours of currentDate as string) & "-" & (minutes of currentDate as string) set newFileName to text 1 thru -5 of attachmentName & "_" & dateString & ".pdf" save eachAttachment in (saveFolder as string) & newFileName
内容的提问来源于stack exchange,提问作者Mondy
相关产品推荐
相关产品推荐

