Keyboard Maestro Macro for Markdown Headers — Flow Control

March 01, 2012 by Gabe | [mmd] |

Here's a quick macro that demonstrates a couple of convenient features of Keyboard Maestro.

The macro works on selected text. It pops open a user input window and asks for the Markdown heading level to use. In this case I want an H3 header.

The result is to convert the selection to an H3 Markdown element. With one macro hot key I can convert a selection to any heading level. The process looks something like this:

  1. Select text
  2. Opt-Shift-H
  3. Type 3 and then return.

It converts this:

My Text

To This:

###My Text###

The macro has two interesting features. The first, is the flow control "While" action. This action executes the nested actions and then evaluates the "Until" condition at the end. The "While" will continue to loop until this condition is matched.

The second interesting feature is the exit condition for the loop. It is dependent on a calculation using a variable defined in the User Prompt action. The combination of these features means that the macro can accept an integer and use as the number of loop events.1


  1. Note, I've excluded any error checking (like if something is even selected) and clipboard cleanup. Clipboard clean appears to be broken with the latest Launchbar nightly build.