Selecting Mac Mail message using Applescript

I am trying to write a line of Applescript which begins after a Mac Mail mailbox search has been executed. I want the Applescript to select the first message in the list so that message will be interpreted as the select by the next Applescript step.

This syntax fails. What else might I try?

tell application “Mail”
set selected messages of first message viewer to first message
end tell

I tried the same some time ago but couldn’t make it work. Now tried again and still no joy.

This script does select a message, but it’s not the first one that’s shown in the UI. Tried whether it has something to do with the current sort options but couldn’t figure it out.

Then found this post on the Script Debugger forum which says …

they’ve been failing to fix AppleScript bugs in Mail for well over a decade

… so I guess this could be a bug too.

-- Mail.app - Select first message
-- Tested with Mail.app version 12.4

-- NOTE: This code does not select the desired message over here, i.e. it doesn't work

tell application "Mail"
	try
		set theWindows to message viewers
		if theWindows = {} then error "Please open a window"
		
		set theWindow to item 1 of theWindows
		
		set theMessages to messages of theWindow
		set selected messages of theWindow to {item 1 of theMessages}
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Mail" message error_message as warning
		return
	end try
end tell

You coud try to use this UI script

-- Mail.app - Select first message (via UI scripting)
-- Tested with Mail.app version 12.4

tell application "System Events"
	tell process "Mail"
		select row 1 of table 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1
	end tell
end tell


Much appreciated @pete31

The UI script does indeed work

For those who may be interested, I am working on an automation so that if I am viewing email through a web app (such as Office 365 OWA) and I want to save the message with attachments in Devonthink, I can do that quickly with Popclip. I highlight a unique part of the subject or message body, Popclip puts that into the system clipboard and loads a Keyboard Maestro macro, and Keyboard Maestro searches for and selects the message in Mac Mail and activates the Devonthink message/attachment script.

It works - though I am tweaking the Pause and I am considering if I should restrict the search content to the Subject and revise the search scope accordingly.

Thanks Pete!

Popclip Snippet is here (just highlight this with Popclip and then choose to Install when Popclip offers):

#popclip
name: Email to Devonthink
icon: square ED
before: copy
url: kmtrigger://macro=Email%20to%20Devonthink
requirements: []
required apps: [com.google.Chrome]
description: Searches in Mail for Highlighted Text
#end

Keyboard Maestro macro is here (edit for the location of the standard Devonthink message/attachment script in your system):

Email to Devonthink.kmmacros.zip (1.8 KB)