I’m writing a Mail Rule that highlights important Gmail messages in Apple Mail. The best I’ve been able to come up with so far is:
-- Copyright 2022 Google LLC.
-- SPDX-License-Identifier: Apache-2.0
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- uncomment when running interactively
--tell application "Mail"
-- set theSel to selection
-- set theMailbox to "email@example.com"
-- set theRule to "Rule 2"
--end tell
--
--using terms from application "Mail"
-- perform mail action with messages theSel in mailboxes theMailbox for rule theRule
--end using terms from
-- end uncomment when running interactively
using terms from application "Mail"
on perform mail action with messages messageList in mailboxes mbox for rule aRule
repeat with theMessage in messageList
set received to date received of theMessage
repeat with importantMessage in messages of mailbox "Important" of container of mailbox of theMessage
if (id of importantMessage) = (id of theMessage) then
log "important"
set background color of theMessage to yellow
exit repeat
end if
if (date received of importantMessage) < received then
log "out of candidates"
exit repeat
end if
end repeat
end repeat
end perform mail action with messages
end using terms from
It works, but it can delay incoming mail by several seconds, and makes Mail less responsive to typing as it’s running. I’ve tried two things to speed it up, which have not worked:
-
looking up messages by ID: the script doesn’t compile. I’ve posted this as Unable to look up messages by id from mailboxes in Mail
-
using a filter expression:
-- Copyright 2022 Google LLC. -- SPDX-License-Identifier: Apache-2.0 using terms from application "Mail" on perform mail action with messages messageList in mailboxes mbox for rule aRule repeat with theMessage in messageList local importantMessages tell mailbox "Important" of container of mailbox of theMessage set importantMessages to (messages where id is (id of theMessage)) end tell if not importantMessages = {} then set background color of theMessage to yellow end if end repeat end perform mail action with messages end using terms from
which works when run interactively, but hangs Mail when run as a Mail Rule.
Do you have any ideas why these have failed, or ways I can make the original script work more quickly as a Mail Rule instead?