AppleScript – Using It to Open Mail by MessageID

applescriptmail.app

Using the AppleScript below I am unable to open the message ID. This same message ID is in a note in my Reminders app and clicking on the link works correctly there.

set emailID to "message://<DB43DAF1-7B87-4DE5-99DB-EC11CB0F6FF7@gmail.com>"

tell application "Mail"
    open location emailID
end tell

I get the pop-up error

The operation couldn't be completed.

(MCMailErrorDomain error 1030.)

Mail was unable to open the URL "(null)".

I've researched that error code but the answers don't seem applicable. Also, when removing the word "location" from my AppleScript, Mail will open but with a blank new email.

Any help getting this script to actually open the email with that ID would be greatly appreciated.

Best Answer

Mail is not smart enough to just tell it to open a document by just its message id in the manner you're trying to do it. You need to tell Mail where to look for it, as in what mailbox it's in.

The following example worked for me when MessageID was set to one in my Inbox using its proper message id however in this example I'm using the message id shown in your OP.

set MessageID to "DB43DAF1-7B87-4DE5-99DB-EC11CB0F6FF7@gmail.com"

tell application "Mail"
    activate
    open (first message of inbox whose message id = MessageID)
end tell

Note that the example code above is limited in that it doesn't gracefully trap an error, so the example code below adds a try statement with an error statement to handle an error gracefully.

set MessageID to "DB43DAF1-7B87-4DE5-99DB-EC11CB0F6FF7@gmail.com"

tell application "Mail"
    activate
    try
        open (first message of inbox whose message id = MessageID)
    on error eStr number eNum
        display dialog eStr & " Number: " & eNum buttons {"OK"} default button 1
        return
    end try
end tell