Stateful Macros -- Performing different hotkey action depending on times pressed

Hey all, I have a hotkey trigger which I used to launch an app (see screenshot). The desired behavior is:

  • If the app to launch is not at the front, bring it to the front
  • If it is at the front, cycle through other apps attached to the key mapping

The state here is that hitting the key trigger should reactivate whatever app the macro last activated. Just bring it back to the front. However when the app is already at the front hitting the trigger repeatedly cycles the apps attached to it.

I have this working, its just quite messy and feels like a hack because anytime I want to add a new app, or swap out existing apps, I have to disentangle the whole thing.

The concrete example:
Hotkey trigger should bring the last-used text editor to the front. When the text editor is at the front it should cycle through: VSCode, Vim, Emacs.

This feels a bit like a meta programming problem. I want to treat the macro itself as data. If I could define a list of three macros (open vscode, open vim, open emacs) and iterate over them I think this would work. But afaik there's no way to use macros as data? I know these are all actual XML data on disk somewhere, but was hoping not to keep things in the GUI if possible (not opposed to some scripting, but the KM GUI is helpful in many ways).

Oh I'll also add another example I use this for, since the multiple-text editors may be niche, even for KM users.

Cycling through browsers:
Hotkey reactivates the last used browser if no browser is focused. If a browser is focused it cycles through them. (chrome->brave->firefox->[repeat])