Any way to "Add Link" to text within KM?

Hi All – wondering if anyone here knows of a way to replicate the “Add Link” (cmd-K) functionality of the cocoa text editors directly within Keyboard Maestro? I would like to take a plain-text URL, add that URL to the text as an embedded link, then paste it as a hyperlink into a .rtf document.

Thanks.

P.S. (Given that this is my first post on the forum, I’ll take a second here to thank you, Peter, for a dynamite application.)

I don't know of any good way of doing this, but here is a hack way to works for the most part.

Basically, it takes the URL and the text, and writes them to a temporary RTF file, and then reads that RTF into the clipboard.

There are so many gotchas and caveats with this though.

The main usage caveat is that the text must contain only valid stuff that can go raw in an RTF file. Basically, I wouldn't try much other than plain letters and spaces (probably numbers are OK). Anything else and you may have to encode it and frankly I have no idea how you encode things in an RTF file.

The macro is relatively straight forward - write the raw RTF data to a temporary file, read it to the clipboard. However it is surprisingly hard to actually construct this macro for a number of technical reasons.

  • The RTF is in a text token processed field, so all the backslashes (of which there are lots) have to be doubled.
  • Keyboard Maestro wont let you write raw text to a file with extension "rtf". If you set the extension to rtf it sets the format to RTF (which means you would get formatted RFC codes, not helpful!). If you change the format to Plain Text, it changes the extension to "txt". So you have to force the issue by putting the path (or at least the extension) in a variable.
  • The Write file (like most fields) has smart quotes substitution enabled, so the quotes get changed from valid straight quotes to curly quotes if you look sideways at it.

But after all that you end up with the link text on the clipboard, then you can do whatever you want with it, for example:

Create Link.kmmacros (3.6 KB)

Hi Peter – thanks for that inventive solution. It sent me down a rabbit hole of exploring the code that lives inside .rtf files, which was educational at the very least.

Don’t know if it’s doable and/or of interest to you, but please consider this a feature request – for an action to “Add Link” to a line of text, ideally covering situations where the TEXT or the URL or both are variables or contained in clipboards.

Thanks.

It is certainly possible as a built in action, but I’m not sure there is wide enough usefulness for this to be worthwhile - probably it is better done as a Plug In action.

1 Like

I just ran across this thread from a search, and thought I'd add, for future reference, that @ComplexPoint creates a RTF hyperlink in AppleScript in this post:

I also want the ability to automatically add a hyperlink to a typed work. In the example below, if I type the word "Bug Tracker", the macro selects the 2 words, presses Command-K, pastes the URL I need into the dialog box, and then presses the Return key. It works, but I also wondered (and searched) to see if this was a standard function in KM to be more elegant. I looked at @JMichaelTX JMichaelTX suggested macro by @ComplexPoint but could not figure out how to make it work for my "seemingly" simple purpose.

I just read a bit higher up and used @JMichaelTX “Create LInk” macro, and it works great for my uses.
Thank you!

I'm not sure if this is the macro that @jdevaud is referring to, but the below macro will create a rich text hyperlink from any web page, using either the selected text or the page title as text of the hyperlink, and the page URL as the URL. You could easily change this macro to prompt you for the text and/or URL.

###Macro: Set Clipboard to RTF Hyperlink & Plain Text MD Link