You don’t have to know AppleScript to create a Keyboard Maestro macro. Instead, learn how to build useful macros that automate mundane tasks for you.
Assign Hot Key to Cancel All Macros
When building a macro, the expectation and result may differ. This global hot key ensures that you can cancel all the running macros to prevent damage to your works, escape infinite loop — thus avoid the urgency to restart your Mac.
Build the Simplest Macro
The point is to be familiar with the available actions in Keyboard Maestro. My first macro resizes an image in Photoshop into multiple sizes and exports it to Desktop. As I grow accustomed with the actions library, I expand the macro to batch processing all the images from the source folder, rename them orderly, and save them into result folder.
Start with something you do every day. It can be replying emails. It can be renaming documents. See if you can turn the process from few steps into single macro.
A Macro Better Produces One Result
Be cautious when you have more than one result in a macro. It might be the indication of bad macro design. Macro that produces more than one result is unpredictable, and highly difficult to debug. In other hand, macro that produces one result, like assigning clipboards to variables, can be easily reused in other complex macro.
In my previous post, I shared an example of a complex macro built from some smaller macros. These smaller macros produce only one result as listed:
Preferencesstores macro configurations.
Keep Writingspeaks the quote when user press
Toggle Keep Writing Modetoggles the macro group.
When you find it difficult to produce an expected result, break the macro into smaller macros.
Store Values into Variables
Variable is where you store the permanent value where you use in almost any fields in Keyboard Maestro. It’s handy when you want to reuse the value in other macros, shell scripts, or AppleScripts.
In the example above, I save the file path in a variable known as
Quotes Path. I later read the file path from the variable, randomly pick one line from the list of quotes, and save it to a new variable called
Quote which is used by the Keep Writing macro to read out the text and displayed in notification.
Another common use case, without involving scripting, is when you want to control macro flow which I’ll elaborate more later.
Access Clipboard History
Clipboard is the common gateway to move values from one application to another. You can access clipboard history during runtime, but it’ll disappear when you log out or restart your computer. Enabling
Save Clipboard History to Disk under preferences save your clipboard history across restarts and log outs.
Access the clipboard history by entering
%PastClipboard%1% in the macro actions text field. The number indicates the record of past clipboard. If you want the sixth record of clipboard from the past, enter
%PastClipboard%6% as the value.
Use Control Flow Actions
Sometimes you want a macro to run only when certain conditions are met. Let’s use the macro that toggle Google Chrome below as example.
This macro lets me switch Google Chrome with hot key combination, but only when Google Chrome is running — in other words, Google Chrome stays inactive even when I’ve accidentally triggered the macro.
You can minimize the amount of mistakes with action flows when building a complex macro. The library can check most situations — from application, menu item, and network name — but when the condition you want is not available, you can use this two methods to control the flow:
- By variables/clipboards value. Assign value to variable. In Keep Writing macro, a user may cause the macro to read out the quotes simultaneously by pressing
deletekey multiple times. To prevent that from happening, I have the macro to check the value of
False— and skip the macro if value is
- By screen image condition. Less ideal for flow control because you must test the accuracy intensively. It tells the engine to pause running macro until the screen contains your selected image. Take the screenshot of the successful dialog, or the green alert, and use them in this condition.
It’s Your Turn
This post briefly touches the surface of creating useful and reusable macros. Become familiar with the Keyboard Maestro actions so you know whether a task can be carried out by macros.