Linking to an email

If you create a note or a to-do in a note taking app for an email in the Inbox of Mail it makes sense to refer to the original email. 2 months ago John Vorhees ran a story in MacStories about creating a MarkDown link in Shortcuts to any email in Mail. I'll offer a variation of the AppleScript below with correct encoding and an Alfred workflow.

I need an url handler for Mail first. A url handler is a link to another application which can trigger an action. The url handler for Mail to open an email is the following:

message://<message ID>

The message ID is a unique identifier for each email. Almost all emails have a message ID. Some emails from spammers don't have a message ID. But I don't want to make a note for a spam email. Using the message ID also has the benefit that the mailbox of the email doesn't matter.

The message ID itself has the form of

[email protected]

The form is neither guaranteed nor does the form matter. The message ID just needs to be unique enough to identify the email.

A Markdown link to the email has the form of 

[Message subject](message://<[email protected]>)

Making an AppleScript with the link to the email

With the previous scripts from this blog I have all building blocks in place to create the AppleScript I want:

  1. Get the selection in Mail.
  2. Get information about the selected email.
  3. Url encode the data in AppleScript.
  4. Copy the finished link to the clipboard.

Here is the finished script:

use framework "Foundation"
use scripting additions

tell application "Mail"

--select an email
set SelectedMessages to (get selection)
if SelectedMessages = {} then
display dialog "Please select an email first!"
end if
set theMessage to first item of SelectedMessages

--add <> and url encode the message
set MessageID to "<" & message id of theMessage & ">"
set MessageID to my urlencode(MessageID)

--add url handler
set MessageID to "message://" & MessageID & ""
--open location MessageID

--make final link to email
set theSubject to subject of theMessage
set theLink to "[" & theSubject & "](" & MessageID & ")"
set the clipboard to theLink
return theLink

end tell

on urlencode(input)
set rawURL to current application's NSString's stringWithString:(input)
set allowedCharacters to current application's NSCharacterSet's alphanumericCharacterSet()
set theEncodedURL to rawURL's stringByAddingPercentEncodingWithAllowedCharacters:(allowedCharacters)
return theEncodedURL as Unicode text
end urlencode

Link to script

When getting the message ID from the email the enclosing < and > are stripped. Therefore, they need to be added back. In other scripts for getting the link to the email in Mail you will see %3C in front and %3E behind the message ID. But that's just the encoded < and >.

Urls have special characters for spaces and similar. In the article about advanced topics for exporting emails out of Mail I already showed a function with the wonderful name stringByAddingPercentEncodingWithAllowedCharacters to encode urls. With the function I don't need to test if any character behaves or not. The function takes care of everything.

I've left "open location message ID" as comment in the script for testing. Then the Markdown link is assembled with the [] brackets around the subject followed by the link in () brackets.

Finally, the link is copied to the clipboard.

Doing a test with my note app Joplin:

A link to the Alfred workflow is available at the Xojo + Alfred page.