Activating and Deactivating Timer Triggers

This is an obvious one, but how do I start and stop a macro that is triggered with a periodic timer.

If enable or disable the macro, does that start or stop the timer?


I think you have three questions there, and I think I can answer each of them. Hopefully correctly. :slight_smile:

If you want to stop a macro that's running, there are multiple ways to do it. In my opinion the simplest method, which I use every hour, is to stop all macros by pressing a key that you have made into a hotkey for a macro that simply runs the action "Cancel All Macros." It's the first macro I create whenever I install a copy of KM in a new Mac. It's very important to me to prevent macros from running forever, especially if they reach some sort of infinite loop and/or grab all my keys and buttons.

If you disable the macro, then none of the macro's triggers will work, including a Periodic Timer trigger. (BUT disabling doesn't stop any copies of the macro that are already running.)

Regarding "wanting to start a macro that is triggered with a periodic timer"... if this is something you want to do regularly, then you can create a hotkey trigger for that macro, which will work in addition to your periodic trigger. I do this a lot myself. For example, I might want a macro to run daily, plus whenever I press a certain hotkey. It works great.

Here's an example of a macro that makes a beep every hour during the day, and also beeps when I press a hotkey.

1 Like

Thank you, yes!!!!!!!!!!!! :tada:

A question, please—I want to prevent multiple instances of the macro, so I don't have multiple sets of it periodically triggering. Do you know how I would do that?

Thank you.

Take a look at this wiki page:

Thank you - I have, and am familiar with locking down several different macros with the same semaphore—but I don't know how to lock down a single type of macro (if that makes sense).

Thank you,

Are these long running macros?

Thank you for asking. I have a Controller macro that enables an Update Macro. The Update macro runs every five seconds (until the Controller macro disables it). What I want to avoid is the Controller macro being called again to instantiate additional instances of the Update macro.

EDIT: I realise that I didn't answer your question. No, the Periodic macro takes a few milliseconds to run.

It works the exact same way. If you know how to use semaphores to prevent two different macros from running concurrently, that exact mechanism also prevents two copies of the same macro from running concurrently. You need only a single semaphore lock statement at the top of a macro to achieve that.

Bear in mind, that depending on what your flags are on the semaphore, it will either wait for the first copy to finish, or it will abort the copy that ran second. I use both methods. The choice is up to you.

Toward the end of the wiki page:

  • You can also cause all additional macro instances to be immediately cancelled by setting a very short timeout (1 hundredth of a second) for the Semaphore Lock Action. When the Semaphore Local Action times out, it cancels the pending execution of that macro instance.
1 Like

Thank you for pointing me to this. I'm embarrassed to admit I don't quite understand what is being said here. Could you please explain it to me…I'm a little lost. Thank you, kindly.

No problem. Actually, it's easy because there's nothing for you to do. If you already have a semaphore at the start of your macro, it will already work to prevent two copies of the macro from running concurrently.

Any macro that starts with a Lock Semaphore will prevent itself from running twice. Although there is a slight difference depending on whether you have the flag "Timeout Aborts Macro" set.

I don't need to see your macro, but I want you to explain exactly what your Semaphore Lock action says. What exactly is your "name" in the action, and what flags do you have checked off when you click on the cogwheel at the upper right of the action?

No worries @notsteve! There's no reason to be embarrassed, we're all here to learn.

Using the action (gear) :gear: menu on the Semaphore Lock action, you can change these settings:

  • "Set Action Timeout..." to 1 hundredth of a second
  • "Timeout Aborts Macro" to checked (✓)
  • "Notify on Timeout" to not checked (x)

As a result, the macro with that Semaphore Lock in place will run once and only once.
Not only will instances of the macro not run concurrently, but if an instance is currently running any further triggered instances will (nearly instantly) cancel themselves.

1 Like

Got ya - thanks!