Setting the selected messages in Mail.app

Puzzled about this syntax problem: these two snippets work fine:


tell application "Mail"
	set x to selection
end tell

==> {
message id 27754 of mailbox “INBOX” of account “JL@mac” of application “Mail”
}


tell application "Mail"
	set x to selected messages of first message viewer
end tell

==> {
message id 27754 of mailbox “INBOX” of account “JL@mac” of application “Mail”
}

However, the next two, trying to set the selection to a particular message ID using the exact syntax returned by the above snippets, do not compile:


tell application "Mail"
	set selection to (message id 27754 of mailbox "INBOX" of account "JL@mac")
end tell

==> Applescript Syntax Error: Expected ", " but found number. (“27754” is highlighted)


tell application "Mail"
	set selected messages of first message viewer to (message id 27754 of mailbox "INBOX" of account "JL@mac")
end tell

==> Applescript Syntax Error: Expected ", " but found number. (“27754” is highlighted)

However, the following works just fine, setting the selection to null, and then back to what it was:


tell application "Mail"
	set x to selected messages of message viewer 1
	set selected messages of message viewer 1 to {}
	set selected messages of message viewer 1 to x
end tell

Anybody know what gives here? Why is the construct "message ID nnnnn of mailbox “INBOX” of account “JL@mac” " a syntax error, when that is what is returned from a “get selection”?

I think one problem might be that ‘selected messages’ expects a list - i found this one’s working for example:


tell application "Mail"
	set selected messages of first message viewer to {first message of mailbox "INBOX" of account "JL@mac" whose id is 27754}
end tell

I know - if the where or whose clauses are used, then there is no syntax error. But when you ask for the selection, it returns the expression "message id xxxxx of mailbox “wwwww” of account “zzzzzz” ", yet using that exact syntax won’t compile, even if you pass it as a list. For example:

message id 27754 of mailbox "INBOX" of account "JL@mac"

==> syntax error

or

{message id 27754 of mailbox "INBOX" of account "JL@mac"}

==> syntax error

or

(message id 27754 of mailbox "INBOX" of account "JL@mac") as list

==> syntax error

I don’t think I ever saw a place where you HAD to use the where or whose construct to specify a record when you know the id and all the other properties. I came across this problem when trying to set the selection to a large list that I had generated of all messages that have attachments:


tell application "Mail"
	set theMasterList to {}
	repeat with theMailbox in inbox's mailboxes
		set theMessages to (messages of theMailbox whose mail attachments is not {})
		set theMasterList to theMasterList & theMessages
	end repeat
	tell message viewer 1
		set visible messages to theMasterList
		set selected messages to every message
	end tell
end tell

This works fine, setting the selection properly to only those messages with attachments.

It’s still a mystery why “first message whose message id is 27754” compiles, but “message id 27754” does not compile.