Can't Get KM to Simulate Option+Tab

I use the Contexts app to provide Windows-like alt+tab functionality on macOS. Basically, tap option+tab to go back to the previous window. Keep tapping it to cycle backwards in time. It's similar to some other apps that do the same thing, and it's functionally similar to macOS's native cmd+tab, but isn't limited to switching between apps.

I'm trying to set up a macro that will lay out the last three windows I had open in three clean columns. I can do it with two windows pretty easily: the "Activate last application" action works robustly enough for my needs.

However, there seems to be no built-in way to switch to the previous two windows. So, I thought I could simulate Contexts's window switching in order to facilitate the macro.

Here we come to the problem: when I simulate opt+tab, KM is simply pressing tab.

The macro (I also tried using a "hold the option modifier, press tab twice, then release the modifier" approach, hence the diabled actions at top and bottom):

Screen Shot 2022-01-25 at 4.07.52 PM

Simulating keypresses also wouldn't work with cmd+tab. It still just simulated tab presses, without the modifier.

As per usual, I am probably missing something simple. Any guesses?

With KM's built-in Switcher Group you don't need Contexts to switch apps(KM can replace many apps).

If you want to control the last three windows of different apps, you should store some related information about these windows, then you can manipulate these windows using KM.

The problem is: you didn't give KM the right information to let KM know which window/windows to manipulate.

Probably Useful Links:


I don't like the KM Switcher Group. Contexts is pretty sleek, especially if you're used to the Windows alt-tab workflow. But, still, I tried using it for this workflow. Unfortunately it sorts every window underneath the app that owns it, and it doesn't sort those by recency, so I can't see how it can solve the task at hand.

I appreciate you trying to solve the deeper problem in terms of window management. I actually searched pretty far and wide before posting my original question, and couldn't find a way to pick up metadata for windows based on recency. I spelunked through those two links but couldn't get the Window Information Tool to work (it seems to need a Security and Privacy permission of some kind, but I can't imagine what it doesn't have permission to do).

Either way, though, my core question still stands: the simplest way to do this would simply be to simulate +tab. Am I doing something dumb there, or is that not currently possible with KM?

You can store window information with a timestamp into a plain text file.
Then you can read window info from file sort by timestamp and open/rearrange specific windows using KM.

As for Security and Privacy Permission, if you don't trust KM, use Little Snitch and fsmonitor to keep an eye on it or use Hammerspoon(open source) instead of KM.

KM will definitely simulate Option-Tab (and most any other keystroke you want it to). I just confirmed it here, and it works as expected. But you do need to grant it the permissions it requests.


1 Like

This may help( though I never used it):

the window with window index (Z-order, negative windows order from the back (eg -1 is last window)).

Sorry, I miscommunicated. I meant that I had already given KM permissions (and Terminal, and Script Editor, and...). So I'm not sure what permission the macro is missing, and it isn't saying, heh.

Weird. So I'm experiencing some kind of bug. I have used KM to simulate all kinds of keypresses, so I'm not sure why alt+tab/command+tab are not working. (To reiterate, I miscommunicated earlier. KM should have all the permissions it needs.)

Thanks. This was what I tried first, but I couldn't get e.g., window index -2 to work.

I'm interested in the logging of recent windows, although that seems like a tedious solution when alt+tab simulation should work.

Try disabling Contexts, then set up a macro with Option-Tab as a trigger: Does that work?


1 Like

I'm confused by this window index now either. :rofl:

Is ther a way to manipulate the previous two or three windows ? @peternlewis

1 Like

Another approach that I've used it to make a dictionary that acts as a "stack". Trigger on "focus window changes" and "push" the app and window into the stack (which is a manual process, you have to move everything in the stack down one. (ahem.. @peternlewis? Push and Pop?) :crossed_fingers:

Then you can get them and do what you want.

1 Like

A couple things:

  • Keyboard Maestro will be simulating the Option Tab keystroke. However that may not trigger another application’s hot keys, or the other application may be looking for the keystroke in a different way, such as scanning the keyboard hardware directly.

  • window index only applies within the context of the selected application (usually the front application). You cannot index between different windows in different applications.

  • You mentioned that you could sort of use Activate Last Application action - there are ways to activate the third most recent application. It’s not trivial, but it is possible, so if that would do (as opposed to third most recent window), then that is something that can be worked through.

Keyboard Maestro does not really have any data structures for which push and pop make sense. For strings, you can just prepend or remove characters already, and there are no other array structures in Keyboard Maestro. But really this is a separate topic.

1 Like

Good idea. I tried to simulate cmd+tab instead, e.g.,

but it doesn't trigger the macOS app switcher either.

I think this makes sense—basically it seems to imply that this simulate keystroke idea is a no-go. I will search for new approaches. Maybe Contexts is scriptable...

That example, Press and Hold Command Modifier, and then simulate a Tab keystroke will immediately release the held Command modifier to simulate the Tab key.

If you are trying to press and hold the Command Modifier and then press the tab key multiple times with the Command staying down, then you need:

  • Press and Hold the Command Modifier
  • Type Command-Tab
  • Type Command-Tab
  • Type Command-Tab
  • Type Command-Tab
  • Release the Command Modifier

Typing a keystroke will release and modifiers that are not needed for the keystroke.

Interesting, thanks for the walkthrough!

I could use what you've described to interact with the native macOS app switcher. However, it still wouldn't work with alt+tab for Contexts. I suspect you were right when you said...

And, sadly, Contexts is not scriptable.

So, I brute-forced a different option. AltTab is yet another way to do Windows-like alt+tabbing on macOS and, fortunately for me, Keyboard Maestro's simulated keypresses are enough to call AltTab's functionality. So, I configured AltTab with a keyboard shortcut I'll never use (+++, and while AltTab's interface is up I configured space to focus into the selected window), then set a KM macro up to use these keys to switch between recent windows.

The result looks like this:
2022-01-28 12.30.58

A bit frenetic, but good enough for punk rock.

Here's the macro (although it is pretty basic) in case it helps anyone else.

Set up three columns.kmmacros (7.7 KB)

Thanks all for the help!