Creating Rich Links in Keyboard Maestro

I write a lot of things in a plain text format known as Markdown. It provides a set of standardised ways to mark up text for processing into other formats (such as HTML), whereby formatting and other rich content elements are applied. I write all of my blog posts in this format, and also posts for other sites where I publish and contribute. One of these sites is Buy Me a Coffee (BMAC), a site where you can donate to my coffee fund if you like the stuff I do. It is also where I post each month to let everyone know what I have been up to. It regularly surprises me just how much content and support to other Internet denizens I’ve actually provided during the course of a month!

Over the course of the month I jot down notes in Drafts, and then in the following month I write these up, in Markdown ready for posting. Unfortunately, BMAC does not support Markdown, but it does support rich text.

A Blanket Approach in Drafts

Fortunately for me, Drafts supports creating rich text from Drafts content, and I just so happen to have an action in the ThoughtAsylum - Export/Import/Share action group that supports that.

TAD-Copy as Rich Text
Converts Markdown to rich text and places it on the clipboard. If no text is selected, the content of the entire draft will be converted and copied. If a text selection is made, then only that selected text will be converted and copied to the clipboard. The styling is based on the definition of the STYLE template tag.

This means that I can generally just run this on my monthly draft, and paste the results into my BMAC post.

However, sometimes I want to add extra bits as I review my post, and in particular, the links prove to be fiddly to work with. This lead me to look into creating a small Keyboard Maestro macro to help me out, and it can be applied to any web site that supports rich text through an editor.

A Targeted Approach in Keyboard Maestro

First of all, let me set out a simple example to illustrate how I use a Keyboard Maestro macro to help me to quickly convert a link specified in Markdown to a rich text link.

An initial piece of text containing a link might look something like this.

I select the text that comprises the link, and then trigger the macro.

The macro grabs the content of the selection, parses it to determine what is the link text and what is the URL, places a rich-text-based linked version of the text on the system clipboard, and then pastes it over the current selection.

The Keyboard Maestro macro being used looks like this, and I keep it in a group of macros that are only enabled when I am using Safari.

There are several interesting elements to the macro, that I’ll cover as I walk through it.

Firstly, I am using ⌘K to trigger the macro as this is a common keyboard shortcut for adding a hyperlink. In the macro, when triggered, the URL of the current tab in Safari is checked to see if it is a post creation URL for BMAC. If it is not, then the key press will be passed on. So, for example, this does not interfere with the use of ⌘K in GMail to create a link. Of course, if we are in a BMAC post page, then the special processing will be carried out.

The macro uses a subroutine I’ve covered before that grabs the current text selection. You can, of course, download a copy of the ‘Get Current Selection’ Keyboard Maestro macro subroutine.

Next a regular expression is used to identify the text and link defined in the selection, and to store them in separate variables.

Once the elements of the link are extracted, I use the textutil command line utility to convert an HTML version of the link (which I build via an echo command) to rich text. This rich text output is pushed directly to the clipboard. After a brief pause, the macro then triggers a paste.

Hopefully, that gives you an idea of how the macro works and how to change it if you want to use it on other sites, or want to use it to convert other Markdown formats to rich text.

Summary

While most of my needs are met via Drafts for this specific scenario, Keyboard Maestro gives me an extra tool on my belt when it comes to quick additions, and for where the content is not starting out in Drafts. While the macro is not particularly large or complex I figured others might find it useful to see how I tackled this, and I’m sure there are other ways too, but this felt pretty clean and streamlined to me. It is also really fast - much faster than the original version I began with which interacted with the UI on the BMAC web page!

I guess, given the nature of this post, I should also point out that if you happen to want to buy me a coffee, you can do so via the button below.

Author: Stephen Millard
Tags: | keyboard maestro | drafts |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read