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...

Leave a Reply

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