Id of incoming messages (Apple Mail)

You can find a message’s id with:

tell application "Mail" to get id of first item of (get selection)

However, this script will throw an error.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with aMessage in theMessages
				try
					
					set xxx to id of aMessage
					display dialog xxx buttons {"Cancel", "OK"} default button "OK"
					
				on error errMsg number errNum
					tell me
						activate
						display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
					end tell
				end try
				
			end repeat
		end tell
	end perform mail action with messages
end using terms from

When is a message assigned its id?
You can replace

set xxx to id of aMessage

with

set xxx to message id of aMessage

and the script will perform as expected.

Is there a workaround to getting the id of an incoming message? If not, are there any unforeseen problems of identifying messages with message id ?

Hi. I can obtain both the ID and Message ID with a rule, however, my (ancient) version of Mail inexplicably crashes when it attempts to display the latter; this only happens when wrapped in the on perform mail action handler. :confused: Can you just use a whose clause to filter for your target messages and avoid the handler?