I noticed that the question in your topic title is not the same as the one in your post:
(1) How to prevent a macro from running twice at the same time?
≠
(2) Anyway to prevent the second instance from starting if the first one is already active?
For question (1) the default Semaphore Lock is sufficient: The triggered macro instances will be queued, but each one will run eventually. (One after the other.)
For question (2) you need either…
- a Semaphore lock with a very short timeout, as explained in point 4 of this section of the Semaphore Lock KM Wiki article, or…
- a Disable Macro action at the beginning and an Enable Macro action at the end of the macro.
Here are four minimal examples to demonstrate it:
The core actions of each macro are…
- a pause of 5 seconds
- a Speak Text action “Action Completed”
To get a picture of the differences, launch each macro with its hotkey trigger, then press the hotkey again 4 times during the 5-seconds pause. (Or more often if you like.)
Normal macro without anything special
[mwe] Pure Macro (Without Anything).kmmacros (2.0 KB)
After 5 seconds you will hear “Action Completed” 5 times, almost at the same time (depending on your speed when you were pressing the hotkey ×5).
With Semaphore Lock (default settings)
[mwe] Semaphore Lock (Default).kmmacros (2.3 KB)
After 5 seconds you will hear “Action Completed” 5 times, in intervals of 5 seconds.
With Semaphore Lock (very short timeout)
[mwe] Semaphore Lock (with Very Short Timeout).kmmacros (2.4 KB)
After 5 seconds you will hear “Action Completed” once.
With Disable/Enable Macro actions
[mwe] Disable:Enable Macro.kmmacros (2.5 KB)
After 5 seconds you will hear “Action Completed” once.
Disclaimer: I’m not using Semaphore Locks very often, so it is quite possible that I’m missing some details.