Advertisement
  1. Computer Skills
  2. Keyboard Maestro
Computers

Working With Text in Keyboard Maestro: Part 2

by
Difficulty:IntermediateLength:ShortLanguages:

Keyboard Maestro is one of the most powerful Mac apps available. Even if you can’t code, you can use it to create macros that automate the things you do. I can barely write a Hello World program in JavaScript, but I’ve automated huge parts of my workflow using Keyboard Maestro.

In the previous three tutorials in this series I’ve looked at how to create keyboard shortcuts, how to use one shortcut for different macros and the basics of working with text. In this tutorial I'll take things even further when it comes to working with text. 

What I’ll do is show you how to build a macro that takes a block of text written with British English spellings and convert it to American English spellings. There will be a few edge cases where it makes a mistake, but with the techniques I show you, you’ll be able to fix them in your own version of the macro. 

Prerequisites

To follow this tutorial you’ll obviously need a copy of Keyboard Maestro. You can grab one from the developers website. It costs $36 for a license and there’s a 30-day free trial. 

You’ll also need to have read the three previous tutorials in this series:

Although you will be able to follow along without doing so, it will also help your understanding if you’ve checked out my previous series on Keyboard Maestro:

What I’ll Build

What I’ll build is a macro that takes a load of text written with British English spellings and converts it to American English spellings. So for example, it would take the string My favourite colour is blue. I idolise blue things. They’re the centre of our world. and return, My favorite color is blue. I idolize blue things. They’re the center of our world.

It’ll do this by searching through the block of text and replacing instances where a word ends with -our, -ise or -re and replace it with -or, -ize and -er respectively. I’ll also add some filters so it doesn’t change They’re to They’er and our to or.

I’ll build it in such a way that any time I find a weird edge case I can quickly fix it. This way I don’t have to research every obscure difference between British and American spellings to get it right first time. 

By the time I’m done, you’ll have learnt some great tricks for manipulating text in Keyboard Maestro. With the techniques you’ll be able to build macros that process text in lots of interesting ways.

Build a New Macro

Create a new macro and call it something like British > American. I’m going to use a Hot Key trigger; in this case Command-Option-Shift-C.

Start building the macro
Start building the macro

The first thing is to get the text somewhere I can operate on it. I’ll do this by copying it to the clipboard. This means that to use the macro you’ll need select the text then hit the trigger.

Add a new Type a Keystroke action from the Text group. Set Simulate Keystroke to Command-C.

the first step
The first step.

I'll start with a really basic feature. Replacing every word ending in -our with the version ending in -or. Since it’s just the ending I’m interested in, I don’t have to worry about creating a separate rule for both colour and valour. Instead, I can use one rule that identifies the end of a word. It doesn’t need to care what comes before the -our as long as it’s followed by a space.

From the Variables group, add a Search and Replace action. These will be the main building blocks of the macro. Ensure Search is set to System Clipboard. Change String (Ignoring Case) to String (Case Sensitive).

search action
Search and Replace action.

For the actual replacement, I'll search for our and replace it with or. There is a space following the suffixes and this is very important as that’s what we’re using to identify the end of the words.

The first rule
The first rule

The problem with this rule is that if it encounters the word our it will replace it with or, so I need a way to fix that. I'll preemptively protect or. I know that my rule uses a space as the break so if I add another character, say a +, in front of the space it won’t trigger it. 

Even better would be to use a string of random characters, say +$+, that is never likely to crop up in anything I write since only Keyboard Maestro is ever going to have to deal with it. 

Add another Search and Replace action before the one that searches for -our and set it to Case Sensitive. I want to find every instance of the word our so enter our for Search. This way it will only find the word when it’s a word on its own. Set Replace to +$+our+$+. Again, spaces before and after the string are important. 

protection ruleThis action stops our from being caught by a later action
This action stops our from being caught by a later action

When the rule runs, it won’t trigger for the word our because there’s no space after, however, I need a way to reset it back to as it was. 

Add another Search and Replace action at the end of the macro. Set this one to search for +$+ (without spaces before and after the string) and replace it with nothing. The reason I’ve left the spaces out and only targeted one of the symbol strings is because I want to be able to add loads of different actions that add them to protected words, but only need one rule to tidy up and remove them.

This action tidies up and removes the protection
This action tidies up and removes the protection

The last thing to do is output the data. To do that, add a Type a Keystroke action and set it to Command-V. This will paste the edited text back in place. Enter a string like that is our colour blue and test it out. 

Now there’s still a lot to do with the macro, but the process is the same. Add Search and Replace actions to do the following:

  • Replace ours with ors
  • Replace ourite with orite
  • Replace ise with ize
  • Replace ises with izes
  • Replace isable with izable
  • Replace isation with ization
  • Replace re with er
  • Replace res with ers
  • Replace ogue with og
  • Replace ogues with ogs

You also need to add variations that take punctuation as a break point instead of a space. In other words, you need a rule that replaces -our, with -or,. Start with a full-stop and comma and add more as you need them.

All these rules will still replace there’re with there’er so add a rule at the top that replaces every instance of ’re with +$+’re+$+.

As you can see, adding each of these additional rules takes about ten seconds at most. 

Once you’ve got all those rules implemented, the macro will work as expected in about 95% of the words to be amended. That means it’s time to start putting it into use. Every time you use it, proof read your work and if you run into a problem, as I did with the word here, add an exception.

demo quick rule
It's quick to add exceptions as you encounter them.

Keep building the macro and within a few uses, it will be tailored to your most common words. 

Wrapping Up

This macro shows how you can use a macro to do a huge amount of text processing very quickly. Since Keyboard Maestro is totally customisable, you can build similar macros to process text in any way you want. 

What I’ve built in this tutorial is the kind of app that would normally require some decent programming skills, but I’ve been able to do it in Keyboard Maestro.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.