With the help of "Ask Different" I got my first AppleScript program working, and I'm trying to improve it. I'm again stymied though.

I have found the message number ID of a particular message in my account's INBOX. AppleScript tells me it is ID 167868. I hope that sounds reasonable, as I have only about 190 messages in my INBOX. I'm assuming that the id specifies exactly which message is concerned.

Now I want to activate Mail, select the INBOX of my account, sort the messages according to the "From:" field and select the message with that id. After that I want to use Mail interactively.

I've been trying to find out how to do this from various AppleScript manuals, but it's all a bit uphill for a beginner, and I would appreciate some help. I tried looking through some AppleScript dictionaries for Mail.app, but I couldn't find appropriate commands, except for "activate". I saw various things in sample code like "menu bar 1", but didn't know how to find the menu items in menu bar 1.

Sorry to ask elementary questions, but I need to get a program working, and this should be only a couple of lines away. After that, I'll be motivated to work more slowly and systematically through some tutorials, so that the level of my questions improves.

  • 1
    You should read about System Events and UI Elements. So you don't have to navigate the elements programatically, UI Browser provides a nice interface to expose the hierarchy.
    – adayzdone
    Commented Nov 24, 2013 at 14:40

2 Answers 2


Probably not the only way to do it... And these are not my favourite type of scripts.

But here is an example of each of the syntax you describe.

Look up:

  • message viewer
  • selected mailboxes
  • selected messages

In the Applescript library for Mail.

tell application "Mail"
    set theMailID to 543521
    set theM to mailbox "INBOX" of account "iCloud"

    set selected mailboxes of message viewer 1 to theM
    set sort column of message viewer 1 to from column
    delay 0.5 -- important delay or message selection may not happen
    set selected messages of message viewer 1 to (first message of (theM) whose id = theMailID)

end tell
  • While this syntactically validates, "set selected messages of message viewer" doesn't seem to actually have an impact on the UI.
    – Glyph
    Commented Feb 15, 2016 at 5:31
  • Does anybody know how to do this in JXA?
    – McShaman
    Commented Apr 4, 2019 at 22:59

Daring Fireball described how to get message IDs in an Applescript.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .