How to perform text manipulation magic with Keyboard Maestro

Have you ever found yourself making the same kinds of edits to text again and again? Many applications offer ways to streamline this process within the confines of the specific application. It gets more challenging when you want to transform text as it moves from one application into another.

Keyboard Maestro — the Swiss army knife of Mac automation tools — to the rescue. Today I’ll walk through a couple of ways you can use it to manipulate text in the clipboard before pasting the text into another application.

Lego magician levitating the word "text:

photo credit: adapted from Eva Peris via flickr cc

These macros use regular expressions. Regular expressions are a powerful pattern matching syntax that can describe even the most complex patterns.

While these macros do the job for me, I’m no regular expression guru. As I wrote this post I removed some redundant elements from my original expressions. I’m sure there are more efficient ways of getting the same result. Let me know if you have suggestions about more elegant solutions.

Automation was the subject of a recent Mac Power Users episode with Sal Soghoian. My one big takeaway from that episode was this: if you find yourself thinking “I don’t want to do this” or “there must be a better way”, it’s time to automate.

These specific examples may not be relevant to you but think about the places in your workflow where you repeatedly modify text. If that text conforms to a pattern, chances are you can use these ideas to reduce friction and save time.

Macro 1 – Tidy up Dropbox paths

Dropbox is the backbone of document storage in my day job. Copying a web link for a Dropbox file is simple but some of my colleagues prefer a standard file path when referencing files.

option +command + c is a fast way to copy the path but it looks something like this: /Users/john/Dropbox (XYZ Inc)/path/to/filename.txt

After deleting /Users/john more times than I care to admit, it dawned on me that this shouldn’t be hard to automate with Keyboard Maestro.

Keyboard Maestro macro to tidy up Dropbox paths

Keyboard Maestro macro to tidy up Dropbox paths

The Search and Replace Clipboard Using Regular Expression (ignore case) is the step that does all the work. Let’s break down what’s going on in this expression:

Part 1: (\/Users\/john)

Here I’m looking for the string /Users/john. The backslashes are necessary to tell the regular expression engine to treat the next character as a literal forward slash. Otherwise, the forward slashes have a different meaning.

Part 2: (?=\/Dropbox \(XYZ Inc\))(.+)

Next, we want to match everything as long as the next bit is /Dropbox (XYZ Inc). A lookahead assertion (?=) will only match if the next group of characters is /Dropbox (XYZ Inc). (.+) takes care of the rest (. matches any character, + means one or more times).

$2 in the replace with field refers to the value of the Part 2 expression. This has the effect of removing Part 1 from the clipboard.

Now I can select a file in Forklift or Finder, press option + command + c to copy the path, and paste it into another application using the keyboard shortcut I’ve defined for the Keyboard Maestro macro (more on that later).

Macro 2 – manipulating markdown references to external websites

When I started writing this blog I settled on the convention of opening links to external sites in a separate browser tab. Markdown makes it easy to write and publish content but it has no mechanism to open in a new tab. This meant converting all links to external sites to HTML. Simple enough, but tedious and error-prone without automation.

It means replacing anything that looks like <a href= target="_blank">link</a> with the equivalent HTML tag such as <a href= target="_blank">link</a>.

Like the first example, this macro uses Search and Replace Clipboard Using Regular Expression (ignore case), but the regular expression is more complex. This time we want to substitute a markdown reference for HTML only when the domain does not match

Keyboard Maestro macro to convert markdown links to external sites to HTML and open in a new browser tab

Keyboard Maestro macro to convert markdown links to external sites to HTML and open in a new browser tab

Part 1: \[([^\]]+)\]

This section matches the link text — the bit inside the square brackets. We’re looking for [ followed by some text, then ]. [^\]]+ matches any single character that is not ] repeated 1 or more times. That expression is enclosed in parenthesis so we can refer to the value of this group later on.

Part 2: \((?!https?://mydomain\.)(.+?)\)

This part matches the URL section of the markdown link but only if it’s not part of mydomain. The complete expression must be in parenthesis. The backslash tells Keyboard Maestro to interpret these as literal characters.

?! signifies a negative lookahead assertion. In other words, the text matches if the next characters are not http://mydomain.. The question mark after the s makes this character optional, so both http and https links match.

.+? matches the remaining text up to the final parenthesis.

Matching expressions are replaced with <a href=$2 target="_blank">$1</a> in the replace with field. This inserts $2 (the URL) and $1 (the link text) into a standard HTML link.

Once you copy a markdown file to the clipboard, running the macro will transform all references to external sites to HTML. This macro alone must have saved me hours of tedium.

Testing and debugging regular expressions

Creating regular expressions that match all the cases you need and don’t give false positives can involve plenty of trial and error. Regular Expressions 101 was immensely valuable when I was building these expressions. Regex101 color-codes the components of the expression and the matching text. It even explains each piece of your expression.

A word of caution — there are some subtle differences between syntax in regular expression interpreters. For example, Keyboard Maestro accepts https?://mydomain\. but Regular Expressions 101 requires you to escape the forward slash characters (https?:\/\/mydomain\.).

Regular Expressions 101 screenshot

Regular Expressions 101 is a great resource for debugging your expressions (click to enlarge)

Triggering the macros

You could assign a separate keyboard shortcut to each of these macros, but I trigger them with a third macro. This way I can use the same keyboard shortcut for all my smart copy and paste macros and use the clipboard value to determine which one to execute.

Keyboard Maestro macro to decide which of the smart paste macros to run based on the contents of the clipboard

Keyboard Maestro macro to decide which of the smart paste macros to run based on the contents of the clipboard

This works for now but I expect some rework as the branches multiply.

Next steps

I use at least one of these macros every day. Their job isn’t that complex, but it makes me smile every time I use them.

Look for repetitive patterns in your work. You probably won’t have a verbatim use for these macros but I bet you could use the same techniques to remove friction from some aspect of your work.

If you’d like to use these macros as a starting point you can download them here. You’ll need to update the expressions to match your domain name and assign a shortcut key combination that works for you.

You may also like...

9 Responses

  1. Manuel says:

    Hi John

    How would you do something similar that i would use in Forklift to open the enclosing folder in the other pane?

    It will use regex to take this fromt he clipboard:

    And delete everything after the last forward slash, in fact deleting the file name and keeping only the path.

    Then I will use the GoToFolder to paste that path in the other pane and that is it


    • John Scullen says:


      Sounds like an interesting challenge. Give me some time to play with it.

      I doubt you even need regex for this. It’s probably just a matter of opening the same folder in the second pane and sending Command-Up Arrow to move to the parent folder.



    • John Scullen says:

      Hi Manuel

      This should get you pretty close:

      Keyboard Maestro macro to open the parent folder of the currently selected folder in the other pane

      Note the regex string is ^(.*\/)(.+)

      It won’t handle the case where you have the root directly selected but seems to work other than that. If it doesn’t execute correctly on your machine trying adding some pause commands between the steps.



  2. Manuel says:

    Hi John,

    Let me give it a try. In the meantime I was playing with this regex


    Because I was running into trouble if the path had 3 or 4 slashes.

    I will try your first. The rest of Forklift commands seems like a better approach than mine, which was trying to use the GoToFolder and pasting clipboard, but it is very inconsistent.


  3. Manuel says:

    It works! Kind of. I have an issue with the clipboard keeping old clips for some reason. What I did to make it work is to manually copy the file path and then run the macro. Then it works. But if I am working with other copy past operations prior to running this macro, it will not work because the clipboard is retaining the last clip PRIOR to the Option-Cmd-C instruction. I am running Copied, a clipboard manager on the background, and also the Maestro Clipboard. I need to experiment disabling these and see. But is almost there!

  4. Manuel says:

    Disabling Copied and deleting the last clipboard does not help.

    Once you apply the macro 2-3 times, the result is unpredictable. The path copied that is then parsed by regex, is usually not the path selected in the file in question. Not sure what is going on… Frustrating.

    • John Scullen says:


      Try adding some pause statements near between the steps where it’s playing up. 0.5 seconds is usually a good starting point. It will be slow but that’s OK while you’re testing and debugging.

      You can also try turning on debug mode in the Keyboard Maestro Engine (select Start Debugging from the Keyboard Maestro Engine icon in the menu bar). This might give you some more insight about where it’s failing.

      Copied or the KM clipboard manager could be a factor. It sounds like the the clipboard could be returning to a previous state. I use the clipboard manager in Alfred and it was running while I was playing around with this.

      Keep experimenting. Sounds like you’re nearly there.



  5. Manuel Alonso says:

    Problem was that the CMD-OPTION-C to copy path to clipboard does not always work. So i changed that for the Menu Action “File, Copy Path to Clipboard”. That has for the most part fixed it, it runs 90% of the times, fails in some intricate deep paths.

    Also, KM clipboard does not let you delete completely clear the clipboard history. It keeps always the last entry. If you check the Alfred clip history, it is emtpy, by KM has always one entry, and no KM actions let you get rid of that. At least I could not.

  6. I had to replace the CMD-OPT-C for the Menu action “File/Copy Path to Clipboard”. The shortcut did not work, probably because some funny clipboard history things that happen with KM.

    KM clipboard does not let you delete the last clipboard entry. It is always there. If you check clip history with Alfred, you can delete them all, but if you do a CMD-V it will paste whatever KM insists in keeping there!

    Anyway, it now works 90% of the times , only fails with some deep paths, 4-5 level deep. Posting my version here.

Leave a Reply

Your email address will not be published. Required fields are marked *