forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-most-recent-email.applescript
executable file
·42 lines (37 loc) · 1.37 KB
/
open-most-recent-email.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/osascript
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Open Most Recent Email
# @raycast.mode silent
# Optional parameters:
# @raycast.icon ✉️
# @raycast.packageName Mail
# Documentation:
# @raycast.description Open the last received email in your inbox in Mail.app
# @raycast.author Ben Yoon
# @raycast.authorURL https://github.com/benyn
tell application "Mail"
if not (first message of inbox exists) then
# Trigger a check for new email just in case.
check for new mail
log "No emails found in your inbox. Checking for new email."
return
end if
# Get the first email from each account inbox and find the one that was received last.
# `first message of inbox` is inadequate since it refers to
# the first message of the first account inbox.
set lastReceivedMessage to missing value
repeat with accountInbox in mailboxes of inbox
if first message of accountInbox exists then
set firstMessage to first message of accountInbox
if lastReceivedMessage is missing value then
set lastReceivedMessage to firstMessage
else if (date received of lastReceivedMessage) < (date received of firstMessage) then
set lastReceivedMessage to firstMessage
end if
end if
end repeat
open lastReceivedMessage
# Show nothing at the end (prevent "missing value" from being shown).
log ""
end tell