如何用AppleScript为Apple Mail指定账户的嵌套邮箱添加子文件夹并修复报错?
解决Apple Mail AppleScript创建子邮箱报错及扩展功能
错误原因分析
- 账户名称不匹配:你需求中指定的目标账户是“Events”,但原脚本里
targetAccountName被设置为“Will”,这会导致无法定位到正确的账户层级,直接触发-10000错误。 - 未处理异常输入:如果用户在弹窗中取消操作或输入空名称,原脚本会直接报错崩溃。
- 缺少选中邮件移动逻辑:未实现将当前选中邮件移至新邮箱的需求。
修复并扩展后的完整脚本
-- 处理弹窗输入,避免空值或取消操作 try set newMailboxName to text returned of (display dialog "Enter the name for the new mailbox:" default answer "" with title "Create Submailbox") if newMailboxName is "" then error "Empty mailbox name is not allowed" on error errMsg display dialog "Error: " & errMsg buttons {"OK"} default button "OK" with icon stop return end try -- 定义目标账户和嵌套邮箱路径(修正为需求中的"Events"账户) set targetAccountName to "Events" set parentMailboxHierarchy to {"Private Events", "Enquiries"} tell application "Mail" try -- 定位目标账户 set targetAccount to account targetAccountName -- 逐层遍历获取父邮箱容器 set currentContainer to targetAccount repeat with folderName in parentMailboxHierarchy set currentContainer to mailbox folderName of currentContainer end repeat -- 创建新子邮箱 set newMailbox to make new mailbox with properties {name:newMailboxName, container:currentContainer} -- 移动选中邮件到新邮箱 set selectedMails to selection if count of selectedMails > 0 then move selectedMails to newMailbox display dialog "Mailbox '" & newMailboxName & "' created, and " & (count of selectedMails) & " mail(s) moved successfully!" buttons {"OK"} default button "OK" with icon note else display dialog "Mailbox '" & newMailboxName & "' created successfully!" buttons {"OK"} default button "OK" with icon note end if on error errMsg number errNum display dialog "Failed to create mailbox: " & errMsg & " (Error Code: " & errNum & ")" buttons {"OK"} default button "OK" with icon stop end try end tell
关键注意事项
- 权限验证:前往「系统设置」→「隐私与安全性」→「完全磁盘访问权限」,确保运行脚本的工具(如脚本编辑器)已被授予Mail的访问权限,否则会触发权限类错误。
- 名称准确性:确认账户名“Events”、父邮箱“Private Events”和“Enquiries”的拼写、大小写与Mail中显示的完全一致,层级路径不能出错。
- 异常处理:脚本新增了try-catch块,覆盖了弹窗取消、空输入、邮箱定位失败等多种异常场景,避免无提示崩溃。
内容的提问来源于stack exchange,提问作者Will Perkins
相关产品推荐
相关产品推荐

