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.
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:
- Using Keyboard Maestro to Create Custom Keyboard Shortcuts.
- Using Palettes to Improve Keyboard Shortcuts in Keyboard Maestro.
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:
- Keyboard Maestro I: Introduction.
- Keyboard Maestro II: Launching Apps Intelligently.
- Keyboard Maestro III: Situational Triggers.
- Keyboard Maestro IV - Control Flow.
- Keyboard Maestro V - Variables.
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.
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.
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.
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.
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:
Where it says 1:, you’re declaring a variable that you want the output to be saved to. Save it as something like AmazonCode.
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.
Finally, add the affiliate code to the link. Add
?tag=YourAffiliateCode to the end of the 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.
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.