apple-mail

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Apple Mail (Read Only)

Apple Mail (仅读取)

Read email via Mail.app AppleScript. No sending or modifying emails.
通过Mail.app的AppleScript读取邮件。不支持发送或修改邮件。

Prerequisites

前提条件

  • Mail.app running and logged in
  • Automation permissions granted (System Settings → Privacy & Security → Automation → Terminal/Claude Code → Mail)
  • If first access attempt times out, ask user to check for macOS permission dialog
  • 已运行Mail.app并登录账号
  • 已授予自动化权限(系统设置→隐私与安全性→自动化→终端/Claude Code→Mail)
  • 如果首次访问尝试超时,请让用户检查macOS权限对话框

Account & Machine Context

账号与设备上下文

See
docs/email-accounts.md
for which accounts are configured on which machines.
请查看
docs/email-accounts.md
了解各设备上配置的账号信息。

Commands

命令

List accounts

列出账号

bash
osascript -e 'tell application "Mail" to get name of every account'
bash
osascript -e 'tell application "Mail" to get name of every account'

Count unread messages

统计未读邮件数量

bash
osascript -e 'tell application "Mail" to count (messages of inbox whose read status is false)'
bash
osascript -e 'tell application "Mail" to count (messages of inbox whose read status is false)'

Recent inbox messages (last 10)

收件箱最新邮件(最近10封)

bash
osascript -e 'tell application "Mail"
  set recentMsgs to messages 1 thru 10 of inbox
  repeat with msg in recentMsgs
    set msgInfo to "From: " & (sender of msg) & " | Subject: " & (subject of msg) & " | Date: " & (date sent of msg)
    log msgInfo
  end repeat
end tell'
bash
osascript -e 'tell application "Mail"
  set recentMsgs to messages 1 thru 10 of inbox
  repeat with msg in recentMsgs
    set msgInfo to "From: " & (sender of msg) & " | Subject: " & (subject of msg) & " | Date: " & (date sent of msg)
    log msgInfo
  end repeat
end tell'

Get message content by index

通过索引获取邮件内容

bash
osascript -e 'tell application "Mail"
  set msg to message 1 of inbox
  return "From: " & (sender of msg) & "\nSubject: " & (subject of msg) & "\nDate: " & (date sent of msg) & "\n\n" & (content of msg)
end tell'
bash
osascript -e 'tell application "Mail"
  set msg to message 1 of inbox
  return "From: " & (sender of msg) & "\nSubject: " & (subject of msg) & "\nDate: " & (date sent of msg) & "\n\n" & (content of msg)
end tell'

Search messages by subject

按主题搜索邮件

bash
osascript -e 'tell application "Mail"
  set foundMsgs to (messages of inbox whose subject contains "keyword")
  count foundMsgs
end tell'
bash
osascript -e 'tell application "Mail"
  set foundMsgs to (messages of inbox whose subject contains "keyword")
  count foundMsgs
end tell'

Search and read first match

搜索并读取首个匹配邮件

bash
osascript -e 'tell application "Mail"
  set foundMsgs to (messages of inbox whose subject contains "keyword")
  if (count foundMsgs) > 0 then
    set msg to item 1 of foundMsgs
    return "From: " & (sender of msg) & "\nSubject: " & (subject of msg) & "\nDate: " & (date sent of msg) & "\n\n" & (content of msg)
  else
    return "No messages found"
  end if
end tell'
bash
osascript -e 'tell application "Mail"
  set foundMsgs to (messages of inbox whose subject contains "keyword")
  if (count foundMsgs) > 0 then
    set msg to item 1 of foundMsgs
    return "From: " & (sender of msg) & "\nSubject: " & (subject of msg) & "\nDate: " & (date sent of msg) & "\n\n" & (content of msg)
  else
    return "No messages found"
  end if
end tell'

Search across all mailboxes

跨所有邮箱搜索

bash
osascript -e 'tell application "Mail"
  set foundMsgs to (messages of every mailbox of every account whose subject contains "keyword")
  -- Note: this can be slow across many accounts
end tell'
bash
osascript -e 'tell application "Mail"
  set foundMsgs to (messages of every mailbox of every account whose subject contains "keyword")
  -- Note: this can be slow across many accounts
end tell'

List mailboxes for an account

列出指定账号的邮箱

bash
osascript -e 'tell application "Mail" to get name of every mailbox of account "Gmail"'
bash
osascript -e 'tell application "Mail" to get name of every mailbox of account "Gmail"'

Notes

注意事项

  • AppleScript
    messages of inbox
    returns a unified inbox across all accounts
  • Messages are indexed newest-first (message 1 = most recent)
  • content of msg
    returns plain text body;
    source of msg
    returns raw MIME
  • Large mailboxes can be slow — use
    whose
    clauses to filter
  • Timeout: use
    with timeout of 60 seconds
    for slow queries
  • AppleScript的
    messages of inbox
    会返回所有账号的统一收件箱
  • 邮件按最新到最旧排序(message 1 = 最新邮件)
  • content of msg
    返回纯文本正文;
    source of msg
    返回原始MIME格式内容
  • 大型邮箱操作可能较慢——请使用
    whose
    子句进行过滤
  • 超时处理:对于慢查询,可使用
    with timeout of 60 seconds
    语句

Self-Improvement

自我优化

If you encounter an AppleScript pattern that fails, a macOS behavior change, or missing guidance in this skill, don't just work around it — fix the skill:
  1. Create a PR from a fresh worktree of
    https://github.com/eins78/skills
    on a new branch, fixing the issue directly
  2. Or file an issue on
    https://github.com/eins78/skills
    with: what failed, the actual behavior, and the suggested fix
Never silently work around a skill gap. The fix benefits all future sessions.
如果您遇到AppleScript执行失败、macOS行为变更,或者本技能中缺少相关指引的情况,请不要仅仅采用临时解决方案——请修复本技能:
  1. 提交PR:从
    https://github.com/eins78/skills
    创建新分支的工作区,直接修复问题
  2. 或提交Issue:在
    https://github.com/eins78/skills
    上提交Issue,说明:失败内容、实际表现以及建议的修复方案
请勿默默绕过技能缺陷。修复问题将使所有后续会话受益。