Here is a Keyboard Maestro action to get a link to the selected message

Continuing the discussion from Learning to Master and "Love" Apple Mail:

Thanks @iPersuade for the link to MacSparky’s TextExpander snippet.

I discovered I already had a keyboard maestro action to do this. I had deactivated it when I was using MailMate.

Apologies for not crediting the original author. If you are them, please let me know.

I can’t attach a .kmmacros file here, but the following can be used in a run script action in Keyboard Maestro (thanks @anon41602260 for the better example of uploading a script here). Caveat emptor, your mileage may vary, void where prohibited by law, sorry Tennessee.

tell application "Mail"
    set _sel to get selection
    set _links to {}
    repeat with _msg in _sel
        set _messageURL to "message://%3c" & _msg's message id & "%3e"
        set end of _links to _messageURL
    end repeat
    set AppleScript's text item delimiters to return
    set the clipboard to (_links as string)
end tell

Hmm, my own script for that is less complicated. Has the advantage of prompting if the user wants the link in standard message://... format or as a markdown link with [message subject](message://...).

tell application "Mail"
		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
			set the clipboard to "[" & subject of theMessage & "](" & theLink & ")"
		end if
	end try
end tell

Just run that script inside a “run a script” action in KM.

(Discourse garbles AppleScript code formatting, but the code above is intact and should be workable.)

1 Like

Strange. message:// insists on opening in MailMate, despite my having Mail set as my default email client.

Deleted MailMate, it’s using Mail now. :man_shrugging:

This preference pane will allow you to set default apps for files, protocoles, URL schemes and so on.

1 Like

Thanks! Should be handy!

1 Like