How to select message in Mail with ID using AppleScript

applescriptmail.app

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.

Best Answer

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