Link to Apple Mail Message Anywhere on Your Mac With Apple Script

Gang,

Here’s a cool post with an AppleScript that lets you create a link to an Apple Mail message anywhere on your Mac.

And here’s a demonstration video.

2 Likes

I might try to see if I can get that to work with Airmail. It would be handy for me if it did.

For Airmail and MailMate (probably others), there is a shortcut:

Command+Shift+Option+C ( ⌥⌘⇧+C )

1 Like

Looks like in Airmail it’s Control-Command-Option-C, not Shift. Thanks for the lead on that though. I will make it part of a Keyboard Maestro or Textexpander shortcut.

1 Like

That AppleScript won’t work with AirMail. Also note the airmail links just work with Airmail on all platforms (but they do work).

Interesting script. I wrote my own version many years ago, which is attached to my Keyboard Maestro menu that opens when I’m using mail. For me, I wanted the option to have either a “plain link” (message://) or a markdown-formatted link that includes the message subject name.

    tell application "Mail"
	try
		if selection is {} then error
		set theMessage to the first item of (selection as list)
		set theID to the message id of theMessage as string
		set theLink to "message://%3c" & theID & "%3e"
		
		set linkType to display alert "Markdown Link" buttons {"Yes", "No"} default button 2
		set answer to button returned of linkType
		
		if the answer is "No" then
			set the clipboard to theLink
		else
			set the clipboard to "[" & subject of theMessage & "](" & theLink & ")"
		end if
	end try
   end tell

Example

[L.L.Bean Shipping Confirmation](message://%3c0.1.14F.58B.1D4866D483CDC16.0@omptrans.e1.llbean.com%3e)

If I have a multi-selection of messages, I have a version of this that will make a list of all the links in markdown format, put the list on the clipboard, for pasting into a markdown-compliant editor.

2 Likes

Here’s your script for Airmail:

tell application "Airmail 3"

set theMessage to the selected message
set theLink to the selectedMessageUrl
set linkType to display alert "Markdown Link" buttons {"Yes", "No"}
set answer to button returned of linkType
if the answer is "No" then
	set the clipboard to theLink
else
	set the clipboard to "[" & subject of theMessage & "](" & theLink & ")"
end if

end tell
2 Likes

I use elink snippet everyday, and I have an idea to make it a little nicer… but I don’t know Apple Script enough to do it.
My idea is that in apps which use hyperlinks (e.g. Word, Omnifocus, etc.) after expanding the snippet we will not see an url (message://…), but something like a hyperlink word. The same thing makes Omnifocus when clipping email message to quick entry (in OF the hyperlink word seen is “Original Message”).
Anyone knows how to change the script to make the snippet work that way?

For the AppleScript heroes: this is script is only good to fetch a message selected on Mail.app main window. This is great for most cases, but sometimes I keep a message open on its own window and close the main window (the list distracts me).

Is there a way to change the script so it will fetch the frontmost window message instead of the selected one main window?

I love this trick but run into issues when I paste the links into web apps (e.g. in an Asana task). Safari doesn’t recognise the link or something so it can’t be clicked. Has anyone found a workaround?

And it seems to be broken with Big Sur

2 Likes

I just tried using this with Big Sur11.1 and it doesn’t seem to be working. Has anyone else experienced this? Is there a solution?

Thank you in advance for your time.

Here’s what I use (in Alfred) - it works fine in Big Sur.

-- Slightly modified version of Efficient Computing's AppleScript: http://efficientcomputing.commons.gc.cuny.edu/2012/03/17/copy-email-message-in-mail-app-to-evernote-applescript/
-- based on https://www.alfredforum.com/topic/4124-copy-mail-url/?tab=comments#comment-24735

tell application "Mail"
	set prevTIDs to AppleScript's text item delimiters
	set clipcat to ""
	set format to "markdown"
	set seperator to ","
	set selectedMails to the selection
	repeat with theMessage in selectedMails
		
		--get information from message
		set theMessageDate to the date received of theMessage
		set theMessageSender to sender of theMessage
		set theMessageSubject to the subject of the theMessage
		set theMessageURL to "message://%3c" & theMessage's message id & "%3e"
		
		--make a short header
		set theHeader to the all headers of theMessage
		set theShortHeader to (paragraph 1 of theHeader & return & paragraph 2 of theHeader & return & paragraph 3 of theHeader & return & paragraph 4 of theHeader & return & return)
		
		--format the message to url
		if (format is "markdown") or (format is "md") then
			set clipcat to clipcat & "[" & theMessageSubject & " @ " & theMessageDate & "]" & "(" & theMessageURL & ")"
		else if (format is "html") or (format is "") then
			set clipcat to clipcat & "<a href='" & theMessageURL & "'>" & theMessageSubject & " &#64; " & theMessageDate & "</a>"
		end if
		
	end repeat
	
	--add the clipboard
	if (format is "markdown") or (format is "md") then
		set AppleScript's text item delimiters to ")["
		set temp to every text item of clipcat
		set AppleScript's text item delimiters to ")" & seperator & " ["
		set clipcat to temp as string
	else if (format is "html") or (format is "") then
		set AppleScript's text item delimiters to "</a><"
		set temp to every text item of clipcat
		set AppleScript's text item delimiters to "</a>" & seperator & " <"
		set clipcat to temp as string
	end if
	set AppleScript's text item delimiters to prevTIDs
	set the clipboard to clipcat
end tell

thanks for this, I am away from my Mac so I can try it tomorrow. I set David’s up to work on a Keyboard Maestro trigger, can this work the same way?

I’d assume so - I only use Alfred, not KM.

Were you able to get the AppleScript to work with Big Sur?

Doesn’t work in Monterey, either. Has anyone seen an updated version?

Here’s mine. Working on Monterey.

tell application "Mail"
	try
		if selection is {} then error
		set theMessage to the first item of (selection as list)
		set theID to the message id of theMessage as string
		set theLink to "message://%3c" & theID & "%3e"
		return theLink
	end try
end tell

Here’s mind (also working on Monterey) that offers two options: give me the link in markdown format, or just give me the message://... link:

tell application "Mail"
	try
		if selection is {} then error
		set theMessage to the first item of (selection as list)
		set theID to the message id of theMessage as string
		set theLink to "message://%3c" & theID & "%3e"
		
		set linkType to display alert "Markdown Link" buttons {"Yes", "No"} default button 2
		set answer to button returned of linkType
		
		if the answer is "No" then
			set the clipboard to theLink
		else
			set the clipboard to "[" & subject of theMessage & "](" & theLink & ")"
		end if
	end try
end tell
1 Like

Thank you! All (mostly) is good now.