Put Mail message link on clipboard

AFAICT, in Mountain Lion, OS X 10.8, the command that put a link on the clipboard for the selected message in Apple Mail has been removed. I assume this is a sandboxing issue. Since that’s a feature I use frequently when I’m making notes in DEVONthink or external editors, I wrote this script for myself as a replacement for the now-missing feature in Apple Mail. The script has a property setting at the top, which controls what the link looks like when it is pasted to your text. You have three options:

  1. The raw OS X message link style: message:
  2. A clickable RTF link that uses the text of the message’s subject line
  3. A clickable RTF link that uses your own text; the dialog offers the existing subject line as the starting point for entering your own text

Instructions are in the comments at the beginning of the script. Over here, I place the script in /Library/Scripts/Mail Scripts, to make the script available for all applications. (There seems to be a ML bug that scripts put into ~/Library/Scripts do not appear in the global scripts menu.)

	Made by korm 20120812 v1
	This script places a link on the clipboard to the selected message in Mail
	Three kinds of link displays (the text pasted to the clipboard) are possible.
	This is  controlled by the value of the p_URLtype property at the beginning of the script:
	"BareLink" is the form "message:<messageID>"
	"RTF" is a text link, in RTF format, which uses the message subject as the clickable text
	"myText" is also a text link, in RTF format, which uses text provided by you at run time

	The following property controls the type of link produced: comment/uncomment only one of these

-- property p_URLtype : "BareLink"
property p_URLtype : "myText"
-- property p_URLtype : "RTF"

tell application "Mail"
	set selectedMessages to selection
	set {o_MessageID, o_Subject} to {message id, subject} of item 1 of selectedMessages
	if o_Subject = "" then set o_Subject to "(No Subject)"
end tell

set o_URL to "message:%3C" & o_MessageID & "%3E"

if p_URLtype = "myText" then
	set UI to display dialog "What text for the URL" default answer o_Subject default button "Cancel"
	set o_Subject to the text returned of UI
end if

if p_URLtype = "BareLink" then
	set the clipboard to o_URL as text
else -- if the property is not "BareLink" then it must be one of the other two options
	set o_HTML to quoted form of ("<a href=\"" & o_URL & "\">" & o_Subject & "</a>")
	do shell script "echo " & o_HTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
end if

You’ve stopped using MailTags, haven’t you? :wink: MailTags adds a Copy Message URL command to the Contextual/Edit menus. Also, FastScripts works fine with scripts placed in ~/Library/Scripts under ML.

I’ll have to give this script a try, just to test the various options-thanks for sharing it.

Yeah, I gave up. :confused: I use three machines and need Mail to look and behave exactly the same on every machine. It’s just way too much trouble to make that happen with MailTags and Mail Act On.

Thanks for the FastScripts tip - I can’t get scripts stored in the global library to work in Keyboard Maestro either. I’m sure it’s a sandboxing issue and KM will eventually fix it.

MailTags now allows you to store your settings on Dropbox, and the same feature is promised for Mail Act-On. How it works is that MailTags checks the secondary location first, and uses those settings first. Indev claims that Mail Tags will even check/sync the settings when Mail is running, so you could make a change on one machine and it update immediately across all your machines. I haven’t tried that myself to confirm.

Since my reply is completely off topic, I’ll reply to Greg by PM. The gist is: it doesn’t work.