Earlier in this book, I discussed AppleScript and Automator, two tools that can control numerous other apps and tie multiple actions together into easy-t0-run shortcuts. Both of those technologies are powerful, free, and included with macOS. But AppleScript’s learning curve precludes casual use, while it’s limited by the capabilities various apps choose to expose. Automator is far easier for a beginner to use, but it, too, has a fairly constrained palette of capabilities—and not all the tasks you might wish to automate fit its “workflow” mold. Meanwhile, apps like Excel and Nisus Writer Pro have fantastic automation capabilities built in, but they’re largely confined to activities within those apps.
So we come to a category of automation tools that—at the risk of overstating my case—transcends these limitations. If you just want to get the job done—not necessarily in the most programmatically elegant way but in a fast, reliable, and flexible way—you want a macro utility. It’s the sort of tool I reach for most often for general-purpose automation tasks.
Like other kinds of tools covered in this book, the idea of a macro utility is straightforward. You pick an action, or a series of actions, from a list; these form the macro’s task. Then you pick one or more events to trigger that action—a keyboard shortcut, a button click, a change in network settings, or whatnot. That’s it: you have a macro.
What’s interesting about the utilities discussed in this chapter is that the lists of potential actions they offer as building blocks for macros are long and diverse. Some of these actions, similar to AppleScript verbs and Automator actions, directly control a particular app (iTunes, Safari, the Finder) or send instructions to macOS (shut down, change display brightness, switch users). Others manipulate behind-the-scenes resources (clipboards, variables, strings) or manage the flow of steps (if/then/else conditionals, loops, subroutines). Still others “play” the visible interface, simulating button presses, menu commands, keystrokes, and mouse movements.
Put all this together and you have a toolkit that—with a bit of cleverness and patient testing—can automate almost any repetitive Mac task that doesn’t require creativity or human intuition. Here are just a handful of examples, all of which can be done with a single click or keystroke:
Having sung the praises of macro utilities generally, I must level with you. For all practical purposes, we’re talking about one utility: Keyboard Maestro. Sure, I’ll mention a few other apps (in Use Another Macro Utility and Switch Contexts with ControlPlane). But in the case of ControlPlane, it aims to solve a conceptually different problem and, to be candid, the others aren’t even in the same league. If you want a great macro utility—and trust me, you do—Keyboard Maestro is where it’s at.
I’ve already given you a taste of what Keyboard Maestro can do, so let me show you what it looks like, walk you through creating a couple of macros, and explore some of its options and little-known features.
When you open the Keyboard Maestro (Figure 46), you’ll see a three-pane Editor interface. On the left is a list of groups, which you can use to organize your macros however you like; this includes the All Macros smart group. In the middle is the list of all the macros in the current group. And on the right is the contents of the currently selected macro (or a blank shell of a macro, if you’ve just created it). To create an empty macro, click the plus button at the bottom of the Macros list.
Within the macro pane (Figure 47), you see two areas: the trigger(s) (top) and the action(s) (bottom). You can configure these two items in any order. A trigger is what you do to make the macro run—a keystroke, a menu command, or a system event, say. (More about triggers in a moment.) The action(s) are what happen when the trigger occurs.
Let’s walk through a few macros to see how it’s all done.
For the sake of illustration, we’ll start by making a macro with a single, simple trigger and a sequence of three actions. When you run this macro, it displays System Preferences > Users & Groups > Login Items. (Ordinarily, you’d have to open System Preferences, click Users & Groups, and then, unless Login Items had been selected the previous time you viewed that pane, click Login Items. So, we’re replacing three clicks with one keyboard shortcut.)
Follow these steps:
Open Login Items
.To do this, click Open in the Categories list and then drag Open a System Preference Pane to the “No Action” label on the right (or just double-click the action). If Users & Groups isn’t already shown next to “Open preference pane,” choose it from the pop-up menu.
So, drag the Pause Until action (from the Control Flow category) underneath the Open Users & Groups Preference Pane action. Click New Condition and choose Front Window Condition from the pop-up menu. Then, from the Front Application pop-up menu, choose System Preferences (if it’s not already there, click More at the bottom to expand the list). Leave the last pop-up menu set to Exists.
Click Interface Control in the Categories list, and add the Press a Button action to the end of your action list. Replace the text OK with Login Items
. At this point, the macro should look like Figure 49.
We’re ready to rock! Press Command-Option-Control-L (or whichever combination you chose). System Preferences should open to the Users & Groups pane, with the Login Items view visible.
Your favorite word processor can probably import and export files in various text formats. But macOS also includes a command-line utility called textutil
that can (among other talents) convert files to or from any of nine different formats. Want to go from Word (.doc or .docx) to HTML (or vice versa)? You can do that with a Keyboard Maestro macro that employs a shell script, and never launch Word at all.
Follow these steps:
Convert Format
.Convert Format
for the Title, and in the Prompt field, type Choose the format you want to convert the selected file(s) to. Must be one of: txt, html, rtf, rtfd, doc, docx, wordml, odt, webarchive.
To
in the first field (the variable’s name) and html
in the second field (its default value).filePath
. Click the plus button next to New Collection, and choose Finder’s Selection Collection from the pop-up menu.textutil -convert $KMVAR_To "$KMVAR_filePath"
At this point, your macro should look like Figure 51.
Now you can run the macro, but first you’ll need at least one document somewhere in one of the supported formats (from Step 6 above). I suggest copying one or more such files to your Desktop to make them easier to work with.
One last thing before we run the macro: if Keyboard Maestro’s status or menu isn’t visible in your Mac’s menu bar, switch back to Keyboard Maestro, go to Keyboard Maestro > Preferences > General and make sure Display Status Menu is set to either Alphabetically, By Group, or Hierarchically (i.e., not Never).
Now select the file(s) you want to convert. Then, choose Convert Format from the Keyboard Maestro status or menu. You should see the dialog (which you created!) (Figure 52).
Leave the To field set to its default, and click OK. A new file should appear with the same name and location as the old file (which will still be there) but with the .html extension—and it’ll be in HTML format! (Feel free to run the macro as often as you like, with different To settings and different files selected to see how it works.)
Here’s a goofy little macro that few people would be likely to use in exactly its current form, but it illustrates a few useful Keyboard Maestro features, and you can certainly adapt it to your own needs. When it runs, it selects all the text on the current line up to and including the insertion point and converts that text to title case (like the heading just above this paragraph). But the trigger is just typing a few characters.
Follow these steps (refer to Macro #1: Open Login Items for details):
Convert to Title Case
.cttc
in the This String is Typed field, with the surrounding pop-up menus set to “case must match,” “match after any character,” and “diacriticals matter.” Also select the “Simulate 4 deletes before executing” checkbox.Title Case
, and close the window. Title Case should then automatically be selected in that pop-up menu.Your macro should now look like Figure 53. (I’ll skip the step of turning off edit mode and showing you what that version looks like, although you can do that if you like.)
To run this macro, make sure your insertion point is at the end of a line. Then type cttc
(without any modifiers). The macro runs, and your trigger characters disappear.
I told you Keyboard Maestro includes a clipboard history. One of my favorite ways to use this is to press a keystroke that pastes whatever was on my clipboard just before the current thing. So, if I copy and paste something, copy a second thing, but then want to paste the first thing again, this is what I use.
Follow these steps, again using earlier instructions as a guide:
Paste Previous Clipboard
.With this macro enabled, simply press Command-Control-V to paste the previous contents of the clipboard.
If you read Automate Microsoft Office, you may recall that in Office apps, you can record a macro. In other words, Office will watch you while you perform activities, and then make them into a macro. You can play this macro back later, no coding required. Keyboard Maestro offers a similar feature. It won’t always produce results as reliable as those you get creating your own macro from scratch—and not every kind of macro can be recorded—but it’s a simple way to ease into macro construction or get unstuck if you’re stuck.
To record a macro:
Now try running your macro. If the macro doesn’t work as expected—which is likely—go back and click Edit to return to edit mode and see if you can modify some of the actions to do what you want them to do. You may also need to add Pause or Pause Until actions to force the macro to wait for your Mac to catch up with it at certain points.
I’ve shown you a handful of actions in the course of walking you through the sample macros. There are many, many more of them. You can learn about actions by looking at the Keyboard Maestro documentation, or by trying them out. Here are just a few actions and categories that I find particularly interesting:
Alternatively, a macro can choose to do something or not based on whether an arbitrary portion of your screen matches an image. As just one example of why this is interesting, a blind reader wrote to tell me he uses this feature, in conjunction with AppleScript, to speak the status of an icon on his screen (enabled or disabled) that he’d have no other way to determine because it’s unavailable to VoiceOver. I think that’s amazing. To learn more about using this action, read How to Assign a Hotkey to Almost Anything by Patrick Welker.
Just as Keyboard Maestro has lots of nifty actions, it has a crazy array of triggers. We’ve seen keyboard shortcuts and commands on the Keyboard Maestro status menu, but there are 29 other options too. I’m not going to enumerate all of them here—you can read all about them in the Keyboard Maestro documentation—but I want to call out a few that I think are especially noteworthy:
Even though Keyboard Maestro is an excellent macro utility for macOS, it’s not the only one. Because I know people will ask, I do want to say a few words about other Mac macro utilities:
That’s not to say Alfred workflows aren’t extremely useful—they are. With a few keystrokes in Alfred, you can create a new note or search in Evernote, perform a search on multiple Web sites at once, or open a selected image in a browser instead of Preview. But they require a particular way of thinking about tasks that doesn’t match the way my brain works, so I find it difficult to recommend them.
The software hasn’t been updated since December 17, 2009. For reference, those were the days of 10.6 Snow Leopard; 10.7 Lion came out in July 2011. Although QuicKeys is still for sale and still sort of, mostly works with Sierra (see extra steps required here), it doesn’t take advantage of any recently added macOS technologies, and has nontrivial bugs. Since it was never updated for Lion, Mountain Lion, Mavericks, Yosemite, El Capitan, or Sierra, I have to assume the current version (4.0.7) is the last one we’ll ever see. It may work (sort of, mostly) for a long time or it may break with the next version of macOS, but despite its power, I can’t feel good about relying on an app that’s so neglected. If development begins again, I’ll be more than happy to update this section of the book!
yKey doesn’t include logic, as such. For example, it can wait for certain app or window states before moving on with the next step in a macro, but it can’t make if/then/else decisions, process variables, perform loops, search for text patterns, or evaluate complex conditions as Keyboard Maestro can. And its interface is odd—it strikes me as being backward from the way most macro utilities approach triggers and actions.
As fond as I am of Keyboard Maestro, there are a few things it can’t do that I need from time to time. For example, I want a certain app to launch whenever I connect my MacBook Pro to AC power, and to quit whenever I unplug it. I want my windows to rearrange in a particular way when I connect a second display to my iMac. And I want to switch my Mac’s audio input whenever I plug in external speakers. It turns out these sorts of tasks are precisely the domain of an app called ControlPlane.
ControlPlane is based on the notion of user-defined contexts, which you can think of as settings or situations your Mac might find itself in. For example, a context could be a location (home, work, or a coffee shop), the presence of a particular connected peripheral, or a mood that might strike you. You begin by creating whatever contexts are useful or meaningful to you.
Then, for each context, you create one or more rules that tell ControlPlane how likely it is that a given context exists. For example, if I have a context “coffee shop,” I might create a rule that says if my current Wi-Fi network is called “Google Starbucks,” my confidence that I’m in a coffee shop is 100 percent. Depending on the context, a single clue may give it away, or you may need a combination of factors, such as which devices are currently connected and what your Mac’s IP address is.
Once the conditions are in place that convince ControlPlane you’re in a given context, it performs any actions you’ve assigned to that context. (It can also perform actions when you leave a context.) So, if my context is “AC power,” with the fact that my Mac is plugged in supplying the sole piece of needed evidence, my action to open a particular app is performed automatically. When I disconnect from power, another action automatically quits that app.
I’ll be the first to admit that this is a rather unintuitive arrangement. And yet, once you get the hang of it, it can become quite powerful, giving you the option to perform tasks based on passive triggers, or combinations of triggers, that are entirely outside the domain of something like Keyboard Maestro.