Advertisement
  1. Computer Skills

Working With Text in Keyboard Maestro: Part 1

Scroll to top
Read Time: 5 min

in the previous two tutorials, I've shown you how to create keyboard shortcuts with Keyboard Maestro and how to get the most out of them by using one shortcut for multiple different macros

One of the most common things you’ll probably use the keyboard shortcuts you make with Keyboard Maestro for is to manipulate and insert text. If you look back at the other tutorials, both examples I used involved some sort of text work. In the first lesson, the macro inserted a default goal. In the second, it set the clipboard to a specific link. 

Sometimes however, you’ll want to do something more with the text. You won’t be able to just insert it as is. Perhaps you need to strip some formatting or add a string beforehand. In this tutorial we’re going to do exactly look at that and consider how you can manipulate text inside Keyboard Maestro macros.

Prerequisites

To follow along with this tutorial you’ll need a copy of Keyboard Maestro running on a Mac. It’s $36 from the developer’s website, although there is a free trial so you can work out whether it’s for you or not before you buy.

You will also need to have read the previous tutorials in this series:

This will be a more advanced look at Keyboard Maestro. As long as you follow the instructions closely, you’ll be fine. If, however, you want a deeper understanding of what’s going on, you should check out my other series on Keyboard Maestro

What I’m Going to Build

For this example, I want to build a macro that takes an Amazon link, strips all the extraneous bits, adds my affiliate code, adds a link to Amazon’s Smile program and, finally, sets it to the clipboard. 

I'll break this down.

Every Amazon link contains a lot of information. It obviously has the link to the product, but it also includes a lot of extra stuff like the search terms you used to find it. 

Check out this Amazon link: https://www.amazon.com/Apple-MacBook-Display-MPXR2LL-Version/dp/B071JNRK1V/ref=sr_1_1?s=pc&ie=UTF8&qid=1504130744&sr=1-1&keywords=macbook+pro.

The only bit that’s actually relevant is the code “dp/B071JNRK1V”. The rest of it is all bits to either make the link more readable to a human or provide Amazon with information. 

Next, Amazon has an affiliate program. This means that if I send you a link to Amazon and you buy the product, I get a small cutback. Obviously, I want to add this to any link I can.

Amazon also has the Smile program. If you visit smile.Amazon.com instead of www.Amazon.com, a small percentage of the sale goes to a charity of your choice. 

As you can see, there’s actually quite a bit of manipulation that has to happen to get an Amazon link into the format I want. These same techniques will let you manipulate text in any other way you want. 

Building the Macro

Create a new macro and call it something like Amazon Links. Give it a Hot Key trigger. I’ve gone with Command-Shift-A.

macro startmacro startmacro start
The base for the macro.

I want to be able to run the macro from any Amazon page. This means I need the macro to grab the URL from your browser. 

In most cases, this just takes two keyboard shortcuts Command-L to select the URL and Command-C to copy it to the clipboard. Add two Type a Keystroke actions to make this happen.

selectingselectingselecting
Grabbing the Amazon URL.

Now that I’ve got the link on the clipboard, it’s time to operate on it. 

I’ll do this using regular expressions. If you’re not familiar with them, regular expressions are a way of matching and selecting information from strings. They’re commonly used in most computer programming languages and they’re also available in Keyboard Maestro.

For more, check out our complete guide. You won’t need to do anything other than copy and paste to get the macro to work in this tutorial. 

Add a Search Clipboard action and in the box where it says, Using Regular Expression, paste the following text: 

1
(?:www\\.)?ama?zo?n\\.(?:com|ca|co\\.uk|co\\.jp|de|fr)/(?:exec/obidos/ASIN/|o/|gp/product/|(?:(?:\[^"\\'/]\*)/)?dp/|)(B\[A-Z0-9]{9})(?:(?:/|\\?|\\#)(?:\[^"\\'\\s]\*))?

Where it says 1:, you’re declaring a variable that you want the output to be saved to. Save it as something like AmazonCode.

regexregexregex
Adding the regular expression.

Next add a Set Clipboard to Text action and paste in https://smile.amazon.com/dp/. From the Insert Token dropdown, select Variable and then the variable you declared in the previous step. You can also type %Variable%AmazonCode% or whatever your variable name is. 

setting clipboard textsetting clipboard textsetting clipboard text
Setting the clipboard text.

Finally, add the affiliate code to the link. Add ?tag=YourAffiliateCode to the end of the link. 

finished linkfinished linkfinished link
The finished link.

And that’s it. The macro now takes an Amazon link, strips the excess, adds an affiliate code and redirects it to Smile. Give it a try. 

finished macrofinished macrofinished macro
And the full macro.

Going Further

This macro just gives you a taste of the kind of text manipulation you can do in Keyboard Maestro. If there’s something you want to do to a string, it’s almost certainly possible and we’ll look at some more options in the next tutorial.

While this macro is now up and running, it will return an error if you try and run it on a non-Amazon page. It also only works with Amazon.com. If you want to go further, you could try modifying the macro so it checks to see that the link contains Amazon before continuing.

You could also create more macros for Amazon.co.uk, Amazon.fr and any other and assign them the same keyboard shortcut. That way, you’ll be able to choose what link to send. 

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Computer Skills tutorials. Never miss out on learning about the next big thing.
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.