AppleScript: Get highlighted Apple Mail message

1.1k Views Asked by At

I need to be able to get the currently highlighted message from the current message thread.

ie

  • thread 1
    • message 1
    • message 2
    • message 3 <- highlighted message
    • message 4

I have tried:

tell application "Mail"
    set messages to selected messages of first message viewer
    set msg to first item of messages
end tell

but this just returns the thread of the currently selected message. It also does not include any outgoing messages in the thread.

1

There are 1 best solutions below

2
On

You don't say what version of Mail or Mac OS X you're using, but that script doesn't run with Mail 10.2 / OS 10.12.2. The error is "Mail got an error: Can’t set every message to selected messages of message viewer 1."

This slight modification works for me:

tell application "Mail"
    set theMessages to selected messages of first message viewer
    set theMessage to first item of theMessages
end tell

If I run it against the message structure you describe then it sets theMessage to whichever one is selected. If multiples are selected then in my testing the order is as expected (from top to bottom), unless "thread 1" is also selected. In that case the order could vary.