One macro instance only

Is there a way to prevent that a macro initiates more at then one at a time?

The situation is this. I want to collect data from the program Pro Tools and i have to use Applescript for this. But System Events in AS and Pro Tools is not a good marriage; it can takes up to 10 seconds before i get data through. It's a known problem and there is no solution found yet. So, i collect this data in the background, and i use the trigger "Focussed Window Changes". But that means that every time i switch window the macro initiates. I could make a manual trigger but that is distracting; data can change and i don't want to think about launching it each time. So, i use the focussed window.
What i tried, is to make a Variable; ThisMacroIsRunning, and cancel the macro when it sees this variable is filled. At the end of the macro i %delete% this variable. But it is buggy; when the macro fails for some reason the "ThisMacroIsRunning" will be filled forever.

So, how else can i make sure a macro instance can only work one at a time?

Yes - use a Semaphore action - see action:Semaphore Lock [Keyboard Maestro Wiki]


1 Like

It's a wonderful action. I use it a lot. The main thing you have to decide is whether you want each call to the macro to be queued up and run sequentially, or whether you want any simultaneous calls to be ignored, which you can make happen using the timeout value on the Semaphore Lock action. If you don't use the timeout, then by default all calls are queued up.

In fact, I even use it multiple times per macro. For example, if one of my macros moves the mouse a couple of times, I start the mouse movement actions with a "Semaphore Lock " KM action. And I end those actions with a "Semaphore Unlock " action. This way if multiple actions are trying to move the mouse, they won't do it at the same time. Semaphores are very useful for safe programming, especially when your macros share a common resource, like a mouse.

1 Like