Files
clawbot/skills/macos-mail/fetch_emails.scpt
T
2026-02-13 13:21:42 -05:00

42 lines
1.5 KiB
AppleScript

#!/usr/bin/osascript
on run argv
set messageLimit to 10
if (count of argv) > 0 then
set messageLimit to (item 1 of argv) as integer
end if
tell application "Mail"
set output to ""
set inboxMessages to (messages of inbox whose read status is false)
-- Fallback to all messages if no unread, or just take top N
if (count of inboxMessages) is 0 then
set inboxMessages to messages of inbox
end if
-- Sort logic is hard in pure AppleScript efficiently, usually they come in order.
-- We'll just grab the first N items which are usually newest.
set msgCount to count of inboxMessages
if msgCount > messageLimit then
set msgCount to messageLimit
end if
repeat with i from 1 to msgCount
set msg to item i of inboxMessages
set msgSubject to subject of msg
set msgSender to sender of msg
set msgDate to date received of msg
set msgContent to content of msg
-- Truncate content if too long
if (length of msgContent) > 500 then
set msgContent to (text 1 thru 500 of msgContent) & "..."
end if
set output to output & "From: " & msgSender & "\nSubject: " & msgSubject & "\nDate: " & msgDate & "\nBody: " & msgContent & "\n---\n"
end repeat
return output
end tell
end run