Unable to look up messages by id from mailboxes in Mail

I’m unable to look up messages by id from mailboxes in Mail (on macOS Monterey, version 12.4). The script:

-- Copyright 2022 Google LLC.
-- SPDX-License-Identifier: Apache-2.0

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Mail"
	tell (get its imap account "Google")
		tell (get its mailbox "[Gmail]/Important")
			message id 5
		end tell
	end tell
end tell

fails to compile with error -2741, Expected end of line but found number. I’m surprised by this; it seems like it should work, from previous posts.

Can I do something to disambiguate between the message class and its message id property?

This seems like some sort of macOS regression.

The problem is that Mail defines a message id property on the message class which collides syntactically with the message id n reference syntax. You can force the issue by falling back to raw syntax:

«class mssg» id 5

But this is not a usable solution because AppleScript converts this back into message id 5 which will fail the next time you try to compile the script.

The problem appears to be confined to Mail’s message class. The ID key form works with other Mail classes:

tell application "Mail"
	iCloud account id "877E1246-6E6B-4EC5-9525-962BBBEBD704"
end tell

Thanks, Mark! I’m able to look up messages using the raw syntax:

			set importantMessage to «class mssg» id (get id of theMessage) of (get mailbox "Important" of container of mailbox of theMessage)

however it does not work as one would hope: performing a lookup by ID against “[Gmail]/Important” returns the message from “[Gmail]/All Mail” whether or not the message exists in Important :man_facepalming:.