1. Code
  2. Coding Fundamentals
  3. Workflow

Keyboard Maestro IV - Control Flow

Scroll to top
Read Time: 6 min


Keyboard Maestro is one of the most powerful and versatile Mac apps available. By creating strings of actions—or macros—that connect different apps you can get almost total control over OS X. In the previous three tutorials I introduced Keyboard Maestro, looked at how to launch apps intelligently and, most recently, looked at how to use situational triggers.

If you’ve been following along with the previous tutorials you should have a good feel for how to create simple macros with Keyboard Maestro. In this tutorial I’ll look at creating much more involved macros using Control Flow actions, one of Keyboard Maestro’s most powerful features.


To make the most of this tutorial you will have need to read the three previous ones: - Keyboard Maestro I: Introduction, - Keyboard Maestro II: Launching Apps Intelligently, and - Keyboard Maestro III: Situational Triggers.

You’ll obviously need to have a Mac with Keyboard Maestro installed. You can get a copy from the developer’s website. It’s $36 for a license and there’s a 30-day free trial. If you’ve worked your way to this point in my Keyboard Maestro series I recommend you skip the trial and just buy the license.

Recipes Not Rules

As before, the macros I’m creating should serve as inspiration rather than rules you have to follow.

If you are new to Keyboard Maestro, following my steps exactly will help you to learn to create your own macros, however once you are beginning to understand the app I recommend you start tweaking what I do so it fits your workflow better.

Treat my macros as recipes that you can follow, substituting in your own apps and requirements as needed.

Control Flow

Control Flow actions are different from regular actions. With them you can add simple logic to your macros. If you want a macro to do one thing when an app is open and another when it isn’t, then you need to use a Control Flow action.

In this tutorial I will be mainly looking at two of the most common forms of Control Flow: the If Then Else and While actions.

  • The If Then Else action works such that if condition A is met the macro will do X, otherwise it will do Y.
  • The While action works such that while condition A is met the macro will do X. Once condition A is no longer met it will stop.

Control Flow actions are one of the key building blocks to make more powerful multi-step macros.

Improved Quick Entry Dialogues

Many apps have features that you can trigger with a keyboard shortcut anywhere on OS X. For example, with Tweetbot you can set up a system wide tweet shortcut.

Similarly, OmniFocus, my task management app, has a quick entry tool I use all the time. It uses the keyboard shortcut Command-Shift-Space. If, however, OmniFocus isn’t running the keyboard shortcut does nothing.

With Keyboard Maestro and Control Flow actions you can create a hot key that triggers an app’s quick entry dialogue if it’s running and launches the app if it isn’t.

I’m will continue using OmniFocus for this example but you can substitute any app you want.

If Then Else ActionIf Then Else ActionIf Then Else Action
The structure of the If Then Else action.

Start by turning off the quick entry keyboard shortcut in OmniFocus’s preferences. You’re going to use this as the Hot Key Trigger for a new macro called OmniFocus Launch Fix.

First, add a If Then Else action from the Control Flow group. The If Then Else action has a dropdown menu to set the conditions and two sections for adding the if and else sub-actions.

For the conditions, set the to trigger if This application OmniFocus is not running.

OmniFocus MacroOmniFocus MacroOmniFocus Macro
The OmniFocus macro you’ll be building.

Under execute the following actions: have Keyboard Maestro open OmniFocus. Then, add a Pause Until action. Have the macro wait until OmniFocus is running.

Next you need to use a Select or Show a Menu Item from the Interface Control menu. With this action you can have Keyboard Maestro select any of OmniFocus’s menubar options. From the Menu dropdown, select OmniFocus then Window and Show Quick Entry.

Finally, have the macro Activate OmniFocus.

Under otherwise execute the following actions add the last two steps from above: selecting the Show Quick Entry menu item and activating OmniFocus.

Now the macro checks to see if OmniFocus is running. If it isn’t it launches the app and then the quick entry dialogue while if it is it launches the quick entry dialogue straight away.

I also use a very similar macro for ScreenFlow, the screencasting app I use for all my Tuts+ tutorials.

Screenflow macroScreenflow macroScreenflow macro
The Screenflow macro I use.

Sorting a Social Media Addiction

To give yourself even more options you can use more than one Control Flow action nesting them within each other. This macro, which tweets “I am wasting my time on social media. #PublicShaming” and quits Tweetbot if I use the app for more than a minute at a time, uses an If Then Else action nested inside a While action.

Create a new macro called Twitter Control. Set it to trigger every time Tweetbot Activates.

First, add a While action that runs if Tweetbot is at the front. Have the macro Pause for 60 seconds then run an If Then Else action.

Twitter control macroTwitter control macroTwitter control macro
The Twitter Control macro you’re building.

The If Then Else action tests to see if Tweetbot is still at the front. If it is, set the macro to Activate Tweetbot then, using a Select or Show a Menu Item action, select Tweet > New Tweet.

Next, use an Insert Text action to enter whatever you want your embarrassing tweet to say.

Finally, use two Type Keystroke actions to send Command-Return to submit the tweet and Command-Q to quit Tweetbot.

If Tweetbot isn’t still in use, you don’t want Keyboard Maestro to do anything. To have the macro ignore the else side of things, add a Break From Loop action from the Control Flow menu.

Obviously Twitter is only one social network. The other major one people become addicted to is Facebook.

Fortunately, I’ve also created a macro that solves this problem. This macro doesn’t have the same public shaming power but also uses two Control Flow actions.

Facebook control macroFacebook control macroFacebook control macro
My Facebook control macro. Notice the similarities between it and the Twitter one you just built.


In this tutorial I’ve covered how to use Control Flow to have a macro perform different actions depending on whether or not a set of predetermined conditions are met. With If Then Else and While actions you can create incredibly powerful macros that only run under very specific circumstances.

They are one of the more advanced features in Keyboard Maestro and are really important for making the most of the app.

In the next tutorial I’ll take a deeper look at another of Keyboard Maestro’s advanced features: Variables.

If there are any areas of Keyboard Maestro you want me to explore or any macros you want me to create in a future tutorial, please let me know in the comments.

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 Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.