# Selecting Mac Mail message using Applescript

**URL:** <https://discourse.devontechnologies.com/t/selecting-mac-mail-message-using-applescript/69916>\
**Category:** Automation\
**Tags:** automation\
**Created:** [March 6, 2022, 1:39am UTC](https://discourse.devontechnologies.com/t/selecting-mac-mail-message-using-applescript/69916 "2022-03-06T01:39:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkaplan](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rkaplan](https://discourse.devontechnologies.com/u/rkaplan)\
**Post date:** [March 6, 2022, 1:39am UTC](https://discourse.devontechnologies.com/t/selecting-mac-mail-message-using-applescript/69916/1 "2022-03-06T01:39:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [March 6, 2022, 3:18am UTC](https://discourse.devontechnologies.com/t/selecting-mac-mail-message-using-applescript/69916/2 "2022-03-06T03:18:31Z")

</div>

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](https://forum.latenightsw.com/t/struggling-to-change-the-sort-view-of-mail-using-applescript/3246/9) 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.

```auto
-- 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

```auto
-- 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

```

---

<div class="post-metadata">

**Author:** ![rkaplan](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rkaplan](https://discourse.devontechnologies.com/u/rkaplan)\
**Post date:** [March 6, 2022, 4:25am UTC](https://discourse.devontechnologies.com/t/selecting-mac-mail-message-using-applescript/69916/3 "2022-03-06T04:25:04Z")

</div>

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):

```auto
#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):

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/8/4/84a5f62cd30e92feccfd00f73e2459a137194c93.png)

[Email to Devonthink.kmmacros.zip](https://discourse.devontechnologies.com/uploads/short-url/hMMO6Ztb0a0SX8waITavVmk3fae.zip) (1.8 KB)

---

<div class="post-metadata">

**Author:** ![system](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/4/4e6ff70c96be2eb307a274d890bba8d71ba671b3.png) [@system](https://discourse.devontechnologies.com/u/system)\
**Post date:** [March 5, 2025, 4:25am UTC](https://discourse.devontechnologies.com/t/selecting-mac-mail-message-using-applescript/69916/4 "2025-03-05T04:25:31Z")

</div>

This topic was automatically closed 1095 days after the last reply. New replies are no longer allowed.
