Help with Rich Text Link Creation from Markdown

OK, I am feeling like a bonehead. I’m trying to use John Voorhees’ Shortcut to create a rich link to a Mail message. (Source: Generating Markdown Links to Mail Messages with Shortcuts and AppleScript - MacStories) It uses an AppleScript, which I am pasting below.

When I run it, I do not get a rich text link, but I get a string of Markdown. It is obviously getting all the right information about the Mail message, but I thought the Shortcut would return a rich text / “clickable” link. Am I misunderstanding the intent of the Shortcut? Am I missing something obvious?

Any help would be appreciated. Thanks!

-Eric

Here is the AppleScript:

on run {input, parameters}
	tell application "Mail"
		set _msg to item 1 of (get selection)
		set _messageURL to "message://%3c" & _msg's message id & "%3e"
		set _msgSubject to subject of _msg
		set _from to sender of _msg
		set _name to extract name from _from
		set the clipboard to ("[" & _msgSubject & "](" & _messageURL & ")" & " from " & _name as string)
	end tell
end run

It looks like you’re generating a Markdown link (looking at the AppleScript code) and saving it to the clipboard, which you could paste into a Markdown document, which would then give you a clickable link within that document.

1 Like

So if I paste it into, say, Drafts, should I expect a rich text clickable link, or a string of Markdown? Sorry I am obtuse.

You’ll get a string of markdown like [link text](https://example.com)

In drafts you can then preview the note and you’ll get a formatted rich text link that you can copy paste where you want it.

1 Like

I’m also trying to use the Shortcuts action “Make Rich Text from Markdown,” but that doesn’t seem to be working. When that action takes its input from the previous action (the AppleScript), and I then put that result onto the clipboard, when I paste, I still get a markdown string. When I set the “Make Rich Text from Markdown” action get its input from the clipboard, I get a generic error message from Shortcuts.

If I understand what you’re doing correctly, your shortcut runs the AppleScript action, which saves the link to the clipboard. Then it runs a Get clipboard action, passes that to the Make Rich Text From Markdown action. If this is what you’re doing, try changing the AppleScript to return the Markdown link (which will pass the link to the next action in your Shortcut) and getting rid of the Get Clipboard action.

Thanks! So I found a variation of the script that ends (before the “end run”) with “return input”. I then added the Shortcut action “Make Rich Text from Markdown” which automatically linked to the magic variable “AppleScript Result.”

Running this combination, when I paste it, alas, I still get the Markdown string, not a rich text link.

Again, I am sure that I am being boneheaded here and missing something obvious. I appreciate folks helping out!

-Eric

After Make Rich Text From Markdown do you have a Copy to Clipboard action?

I have tried it with and without that (“copy to clipboard” action), and get the same result, a Markdown string. I have also tried removing the part of the script that adds the recipient name, wondering if that was messing with the Markdown creation, so that the “set clipboard” line of the script reads:

set the clipboard to ("[" & _msgSubject & "](" & _messageURL & ")" as string)

But I still get a Markdown string instead of a rich text link.

-Eric

Please post the whole shortcut.

1 Like

Here is what I have been using. I can’t claim any authorship, it is John Voorhees, and any mistakes in it surely come from my tinkering. And any failure of it to do what it says is surely due to my lack of understanding.

Thanks to everyone who is helping me out with this!

It appears the rich text is not being copied to the clipboard. I don’t know why, perhaps the “Make rich text” action is unable to parse the Markdown.

If I have researched this correctly a rich text link looks like this:

{\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt Google}}

So I suggest you do something like this:

return "{\\field{\\*\\fldinst HYPERLINK \"" & _messageURL & "\"} {\\fldrslt \"" & _msgSubject & "\"}}" as string

and follow that with a “Copy to clipboard” action.

The AppleScript in this shortcut is making a Markdown link, which works fine to paste into a Markdown-aware editor – it becomes clickable. It might be easier to help you troubleshoot and create a solution to know where you plan to paste the link, as different applications use different link syntax. Then potentially you could build a link in the script that’s more “native” to the target application.