Updated Apple Mail Link AppleScript

For years now, I’ve been iterating on an AppleScript to copy a link for the currently selected Apple Mail message to the clipboard. You can then paste the link elsewhere and get a link back to the source message.

This came up during a recent webinar on the Apple Productivity Field Guide. Since the last time I published this script, I’ve added a few new features:

  • Error handling plus a “Basso” sound upon error
  • Cleaner message description in the alert
  • A slight change to the way the link is created to help avoid link issues.

Enjoy!

tell application "Mail"
    try
        set selMessages to selection
        if (count selMessages) > 0 then
            -- Handle multiple message selection
            set msgCount to count selMessages
            if msgCount > 1 then
                display notification (msgCount as rich text) & " messages selected. Using first message." with title "Mail Link" sound name "Pop"
            end if

            set thisMsg to item 1 of selMessages
            set thisMsgID to message id of thisMsg
            set thisMsgURL to "message://%3C" & thisMsgID & "%3E"
            set the clipboard to thisMsgURL

            -- Truncate sender and subject for cleaner notifications
            set senderText to sender of thisMsg
            if length of senderText > 30 then
                set senderText to (rich text 1 thru 30 of senderText) & "..."
            end if

            set subjectText to subject of thisMsg
            if length of subjectText > 40 then
                set subjectText to (rich text 1 thru 40 of subjectText) & "..."
            end if

            display notification "From: " & senderText & " - " & subjectText with title "Message Link Copied" sound name "Pop"
        else
            display notification "Please select a message first." with title "No Message Selected" sound name "Basso"
        end if
    on error errMsg
        display notification errMsg with title "Error Copying Link" sound name "Basso"
    end try
end tell
Written by David Sparks

I write about getting more from your Apple gear. If this was useful, go deeper in the Labs or grab the weekly newsletter.