I figured there was an AppleScript solution, but I knew it was beyond me, and I hadn't played much with OCR so I thought it'd be fun :). However, the script doesn't seem to work (Sonoma, KM 11.0.3), as it throws this error with a message selected:
Mail got an error: message viewer 1 doesn’t understand the “theName” message.
So I asked my LLM-of-the-day, and it said that you can't assign theName
in that way. It suggested this, which works:
tell application "Mail"
set AppleScript's text item delimiters to " - "
tell message viewer 1
set selectedMessage to item 1 of (get selected messages)
set subjectText to subject of selectedMessage
set theName to text item 2 of subjectText
end tell
end tell
return theName
I have no idea why it fails or why it works, but it does work in the LLM-provided version :).
There are only two disadvantages that I see to AppleScript. The first is that the message has to be selected, which just means I have to click on it before running the macro (not a huge issue). The second is that it's (shockingly, to me) marginally slower than doing OCR, probably due to the overhead of AppleScript.
The AppleScript method takes ~0.17 seconds; the OCR method takes ~0.12, despite having to do a screen capture, OCR, and regex to extract the name. Wild.
Thank you for the AppleScript version—it's probably also much more reliable than my OCR method, but I had fun figuring that one out :).
-rob.