If then else looping Question


I have two Macs right next to each other . . . one has a Presentation software running and the other Mac has Wirecast-a web streaming/recording software on it. The presentation software sends an NDI signal to Wirecast. Wirecast also has three cameras connected that are receiving a signal via SDI for different camera shots.

I have bound specific num-pad keys to each camera and for the presentation shot. 0 is the Presentation, and 1, 2, 3 are camera shots. CMD G makes each shot go live. So if I want to have camera one, I just hit 1 on the num-pad and then CMD G and it will send that to the output.

I have a physical remote control that uses F9 to go forward that has a receiver connected to the Presentation computer and a receiver that's also connected to the Wirecast computer.

Right now, I have it set up that when a slide is advanced in the Presentation, F9 is also triggered in Keyboard Maestro which sets off a nice little macro that hits 0 on the num-pad and then CMD G and after like 15 seconds of pause, CMD G is triggered to go back to the original camera shot.

Everything so far works perfectly. I'm wanting to make it slightly more advanced so that when I type a keyboard shortcut, say cmd shift F6 then it sets off an If then else, where a loop of changing between cameras occurs every 25 seconds or so and then if F9 is triggered, it will switch to the presentation slide for like 10 seconds and then go back to rotating the cameras.

I'm a bit lost trying to make it come out of the loop if F9 is found to be pressed and then back to the normal looping of cameras?

Any help would be super duper amazing!

There's a couple (at least) of ways to do this. And I'm assuming you want the "interrupt" to be pretty much instant.

One would be to have a very quick loop in your macro, testing for the F9 key, and within that loop you would keep time so that you knew when to switch cameras. The quicker you run the loop the more processor intensive it will be, so bear that in mind.

The second would be to have two macros -- the first driving the loop; a second (your F9) that killed the first macro, switched to the presentation slide for 10 seconds, then launched the first macro again -- if you want to resume rotating at the same camera, and especially if you want to resume (rather than reset) the timer on that camera, you'd post values as Global variables.

Which do you think would work best in your setup?

Thank you in advance for your help!

The Wirecast Mac is a 2019 Mac Pro with 3.3 GHz 12-Core Intel Xeon W and 96 GB of RAM. Wirecast with all the cameras running and streaming says the App takes about 11% of the CPU. How much will Keyboard Maestro take up the CPU if I went route one?

I'm totally fine with going either way.

If I went with your second suggestion, how about would I go about setting it up?

Any time you're messing with repeat structures there's a chance of getting it wrong and setting up an infinite loop -- in the worst cases, a loop that won't even let you activate KM's "Cancel All Macros" menu item. In this case we're deliberately creating an infinite loop! So, if you haven't already, make yourself a "Kill" macro -- which can be as simple as:

Kill All Macros.kmmacros (1.4 KB)


So, using "Display text" actions as proxies for your feed switching, here's an example of the first "pause then continue" macro:

Loop With Pause.kmmacros (6.9 KB)


...and the (really simple) "interrupt" macro:

Make Loop Pause.kmmacros (1.5 KB)


The main macro jumps loops forever, displaying dialogs 1, then 2, then 3, then 1, etc, allowing 5 seconds for each. But during each display period there's a sub-loop that checks a global variable every 5/100ths of a second -- if that's been set it displays the "presentation" dialog and then waits for 10 seconds before continuing from where it left off with the main dialogs.

The interrupt macro does nothing more than set the global variable.

The other method would be to cancel the "camera switch" macro and switch to "presentation mode", re-launching the "camera switch" macro when done. Main macro:

Loop With Quit.kmmacros (4.2 KB)


And the interrupter:

Quit Loop.kmmacros (2.1 KB)


...where the main macro posts its instance UUID to a global and the interrupter uses that same global to aim the cancel action correctly. The advantage of this method is that you don't have all the "shall I pause, shallI pause, shall I pause...:" going on -- the disadvantage is that, unless we do more work to save state, the main macro always restarts the sequence rather than carrying on from where it was paused.

Hopefully that explains things and you can work something similar into your own macro. But if I've missed something out, just shout.

It makes sense, thank you so much for your time in helping me! I haven't implemented it yet (tomorrow's work), I had a question though. Would it be much more effort if instead of reverting back to the start of the script and original starting camera, that it could be a random camera? Like randomly select 1-3 when going back to the original loop?

Just use KM's RAND function to set Local_cameraNumber.


Whether you use RAND(0,2) or RAND(1,3) depends on where in the macro you use it, and that depends on the behaviour you want (resume the feed timer, reset the feed timer) -- as do any other changes you'll need to make. Have a play with the examples to see how what you change determines the content and timing of the "Display Text" dialogs.