Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

Mail: reach a message by its id with «class mssg», the mailbox search as fallback

Claudebe3e361fdbee
diff --git a/NotchBuddy/Sources/App/MailController.swift b/NotchBuddy/Sources/App/MailController.swift
index 7cd57224..f81f85ec 100644
--- a/NotchBuddy/Sources/App/MailController.swift
+++ b/NotchBuddy/Sources/App/MailController.swift
@@ -571,7 +571,7 @@ final class MailController: ObservableObject {
"""
tell application id "com.apple.mail"
set mb to mailbox "\(escape(message.mailbox))" of account "\(escape(message.account))"
- set m to message id \(message.messageId) of mb
+ \(MailScript.findMessage(message.messageId, in: "mb"))
\(body)
end tell
"""
diff --git a/NotchBuddy/Sources/App/MailPlan.swift b/NotchBuddy/Sources/App/MailPlan.swift
index a51ffb53..e3d62a0e 100644
--- a/NotchBuddy/Sources/App/MailPlan.swift
+++ b/NotchBuddy/Sources/App/MailPlan.swift
@@ -336,7 +336,7 @@ extension MailScript {
let test = onlyName.map { "(name of a) is \(quote($0))" } ?? "true"
return """
tell application id "com.apple.mail"
- set m to message id \(messageId) of mailbox \(quote(mailbox)) of account \(quote(account))
+ \(MailScript.findMessage(messageId, in: "mailbox \(quote(mailbox)) of account \(quote(account))"))
set out to ""
repeat with a in mail attachments of m
if \(test) then
@@ -354,7 +354,7 @@ extension MailScript {
static func replyDraftScript(account: String, mailbox: String, messageId: Int, body: String) -> String {
"""
tell application id "com.apple.mail"
- set m to message id \(messageId) of mailbox \(quote(mailbox)) of account \(quote(account))
+ \(MailScript.findMessage(messageId, in: "mailbox \(quote(mailbox)) of account \(quote(account))"))
set r to reply m with opening window
try
set content of r to \(quote(body)) & return & return & (content of r)
@@ -370,7 +370,7 @@ extension MailScript {
static func detailsScript(account: String, mailbox: String, messageId: Int, textLimit: Int) -> String {
"""
tell application id "com.apple.mail"
- set m to message id \(messageId) of mailbox \(quote(mailbox)) of account \(quote(account))
+ \(MailScript.findMessage(messageId, in: "mailbox \(quote(mailbox)) of account \(quote(account))"))
set out to ""
try
repeat with a in mail attachments of m
diff --git a/NotchBuddy/Sources/App/MailTriage.swift b/NotchBuddy/Sources/App/MailTriage.swift
index 9b496115..9b581361 100644
--- a/NotchBuddy/Sources/App/MailTriage.swift
+++ b/NotchBuddy/Sources/App/MailTriage.swift
@@ -235,6 +235,22 @@ enum MailSearch {
}
enum MailScript {
+ /// AppleScript that sets `m` to message `id` of `mailbox` (a reference such as
+ /// `mailbox "INBOX" of acc`): straight by its id, without reading the mailbox. Written
+ /// «class mssg» because "message id" is also a property of Mail's messages (their
+ /// Message-ID). If Mail refuses it, the mailbox is searched as before.
+ static func findMessage(_ id: Int, in mailbox: String) -> String {
+ """
+ set m to missing value
+ try
+ set m to «class mssg» id \(id) of \(mailbox)
+ get id of m
+ on error
+ set m to first message of \(mailbox) whose id is \(id)
+ end try
+ """
+ }
+
/// A string inside AppleScript double quotes.
static func quote(_ s: String) -> String {
"\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\""
@@ -311,7 +327,7 @@ enum MailScript {
"""
tell application id "com.apple.mail"
set acc to account \(quote(account))
- set m to message id \(messageId) of mailbox \(quote(mailbox)) of acc
+ \(findMessage(messageId, in: "mailbox \(quote(mailbox)) of acc"))
set rid to ""
try
set rid to message id of m
diff --git a/tests/MailTriageTests.swift b/tests/MailTriageTests.swift
index 8d2b18e2..cba08671 100644
--- a/tests/MailTriageTests.swift
+++ b/tests/MailTriageTests.swift
@@ -93,8 +93,8 @@ enum MailTriageTests {
static func scripts() {
let a = MailScript.applyScript(account: "Pro", mailbox: "INBOX", messageId: 42, action: .junk)
- precondition(a.contains("account \"Pro\"") && a.contains("message id 42 of mailbox"))
- precondition(!a.contains("whose id is"), "a direct reference, not a scan of the mailbox")
+ precondition(a.contains("account \"Pro\"") && a.contains("«class mssg» id 42 of mailbox \"INBOX\" of acc"))
+ precondition(a.contains("get id of m"), "the direct reference is checked, the scan is only the fallback")
precondition(a.contains("set junk mail status of m to true") && a.contains("move m to junk mailbox"))
precondition(a.contains("message id of m"))
let mv = MailScript.applyScript(account: "Pro", mailbox: "INBOX", messageId: 1, action: .move("Clients"))