Hook to Apple Mail message alternates between message and app

Hi - firstly, thanks for a great app!

I’m just getting started, so this is probably user error, but I have a link I’ve added to Things, and when I click on that link it’ll first open the Apple Mail app to wherever it was last time I used it, next click of the link takes me to the specific message, which is what I actually want and then alternates between the two with each subsequent click. Is there a way to make it open the specific message every time?

Welcome to Hookmark forum, @Psionmark .

Sorry about this issue. What’s your macOS version?

If Mail app already open, will clicking on the link open the desired email?

Thank you

Hi - I’m on Ventura 13.5.1.

It does seem a little random. I closed down Mail, then clicked on the link in Things. This opened the Mail app, with the linked message in a window on the side, which is fine. I then closed the specific Mail message, clicked the link in Things again, and it just brought the Mail app to the foreground with a different message highlighted (and not open to the side).

Hope this helps - let me know if you need any more info. If I can get this working, it’d be a huge help as I’m moving to a different email provider which doesn’t currently have a Snooze function.

Update: I’ve updated my MacBook Air to Sonoma 14.1.1 and have the same problem. Also, the problem exists wherever I paste the Hookmark link to, so it’s not specific to Things.

Could you please replace the script in Hookmark → Preferences->Scripts->Mail->Open Item with the script below? Then click the save button and give it a try. Please wait several(3-7) seconds and see if the mail is opened.

Thank you

Summary
set MessageID to "$0"

set MessageID to text 9 thru -1 of MessageID
set MessageID to encodeText(MessageID, true, false)

tell application "Mail"
    activate
delay 2
    try
        set myurl to "open -a Mail \"message:%3c" & MessageID & "%3e\""
        do shell script myurl
    on error eStr number eNum
        display dialog eStr & " Number: " & eNum buttons {"OK"} default button 1
        return
    end try
end tell

on encodeText(theText, encodeCommonSpecialCharacters, encodeExtendedSpecialCharacters)
    set theStandardCharacters to "abcdefghijklmnopqrstuvwxyz0123456789%"
    set theCommonSpecialCharacterList to "$+!'/?;&@=#%><{}\"~`^\\|*"
    set theExtendedSpecialCharacterList to ".-_:"
    set theAcceptableCharacters to theStandardCharacters
    if encodeCommonSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theCommonSpecialCharacterList
    if encodeExtendedSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theExtendedSpecialCharacterList
    set theEncodedText to ""
    repeat with theCurrentCharacter in theText
        if theCurrentCharacter is in theAcceptableCharacters then
            set theEncodedText to (theEncodedText & theCurrentCharacter)
        else
            set theEncodedText to (theEncodedText & encodeCharacter(theCurrentCharacter)) as string
        end if
    end repeat
    return theEncodedText
end encodeText

on encodeCharacter(theCharacter)
    set theASCIINumber to (the ASCII number theCharacter)
    set theHexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set theFirstItem to item ((theASCIINumber div 16) + 1) of theHexList
    set theSecondItem to item ((theASCIINumber mod 16) + 1) of theHexList
    return ("%" & theFirstItem & theSecondItem) as string
end encodeCharacter



Sorry for the delay - the replacement script seems to have sorted it. There is the delay you mentioned, but it does now work as I wish, thanks :slight_smile:

Thank you for letting us know, @Psionmark . I am glad it works for you.

You can adjust the delay time so you don’t have to wait so long. You can change line 8:

delay 2 

to a different delay period, such as
(1)delay 1
(2)delay 0.5
(3)delay 0.1

So you can find the best delay time on your system.

Thank you