Operate the built-in Apple Mail app on macOS via AppleScript/JXA, supporting searching, reading, and composing emails.
Use Cases
- The user says "Help me check the latest emails" or "Are there any emails from XX?"
- The user says "Help me write an email to..." or "Reply to that email just now"
- The user says "Search for emails about project progress"
- The user says "Help me summarize today's unread emails"
Execution Method
Get unread emails
osascript -e '
tell application "Mail"
set unreadMessages to (messages of inbox whose read status is false)
set output to ""
repeat with msg in (items 1 thru (min of {10, count of unreadMessages}) of unreadMessages)
set output to output & "From: " & (sender of msg) & linefeed
set output to output & "Subject: " & (subject of msg) & linefeed
set output to output & "Date: " & (date received of msg) & linefeed
set output to output & "---" & linefeed
end repeat
return output
end tell'
Read email content
osascript -e '
tell application "Mail"
set msgs to (messages of inbox whose subject contains "关键词")
if (count of msgs) > 0 then
set msg to item 1 of msgs
return "From: " & (sender of msg) & linefeed & ¬
"Subject: " & (subject of msg) & linefeed & ¬
"Date: " & (date received of msg) & linefeed & ¬
"Content: " & linefeed & (content of msg)
end if
end tell'
Search emails
osascript -e '
tell application "Mail"
set results to (messages of inbox whose subject contains "搜索词" or sender contains "搜索词")
set output to ""
repeat with msg in (items 1 thru (min of {20, count of results}) of results)
set output to output & (sender of msg) & " | " & (subject of msg) & " | " & (date received of msg) & linefeed
end repeat
return output
end tell'
Create new email
osascript -e '
tell application "Mail"
set newMessage to make new outgoing message with properties ¬
{subject:"邮件主题", content:"邮件正文", visible:true}
tell newMessage
make new to recipient at end of to recipients ¬
with properties {address:"recipient@example.com"}
end tell
-- 不自动发送,让用户确认
end tell'
Safety Rules
- Do not send emails automatically: After creating a draft, have the user confirm sending in Mail
- Do not delete emails: Only read and search
- Privacy protection: Do not log email content
Output Specifications
- Display the email list in table format (sender, subject, date)
- Preserve the original format of email content
- When summarizing unread emails, sort by importance (known contacts first)