Macro is being re-triggered causing problems

I have created an Action to let me rename screenshots as they are sent to the appointed folder. I have pointed the screenshots to a Screenshot folder in my Documents folder. The Action successfully lets me rename the screenshot, but while clicking the OK button causes the dialog box to stutter a little, it does not disappear. In fact, I have to click the Cancel button twice to finally dismiss it (pressing the ESCape key does nothing.) Here is the KM screen for the Action:


I have added a command at the end to show the screenshots folder to assure myself it was successfully saved with the proper new name since the OK button gives me no indication it was activated correctly. If I can get the OK button to dismiss the dialog box, I would drop that last command.

I am using KM 10.0.2 on an M1 Mac mini running OS 12.3.1.

The problem is being caused by your Move or Rename action: when that executes it is in effect creating a new file in that same folder and so it causes your macro to trigger again - hence the apparent “stuttering”.

The solution is straightforward: put a Semaphore Lock action with a short timeout as the first action in your macro. This will prevent the re-triggered macro from executing.

To understand how and why you need to do that read the section in the KM wiki about semaphores here: action:Semaphore Lock [Keyboard Maestro Wiki]


Adding a Semaphore Lock causes my rename macro to fail. Obviously, I am doing something wrong, but fiddling with the timeout values and options doesn't solve the problem. The documentation of Semaphore Lock on the KM site is rather poor.

Did you try setting the timeout to something really short as @tiffle suggested? One hundredth of a second usually seems to do the trick.

Something else I've done a few times, for more complex scenarios, when I want total control over whether or not a macro can run again is:

Stick an If/Else at the start of the macro, set to:

If variable RunCheck is "DoNotRun" then cancel this macro.

Straight after this, set RunCheck to "DoNotRun".

Then at the end of the macro, add a pause long enough for all processes to have completed and then set the RunCheck to "Run".

I would think the semaphore lock should work in your case, but it might be worth a try if not.


Noisneil: I tried your suggestion. The dialog box dismissed properly, but the renaming macro failed to work. I did not get a screenshot with my chosen name, just the standard default name.

You can try the following macro,
This works for me.
[EXP] Rename Capture Screenshot File.kmmacros (4.8 KB)



I have retitled your question, since the issue has nothing to do with the OK button in the prompt failing.

As noted, the macro is being re-triggered, and that re-triggering is what you are experiencing.

Use the Log action to record in the Engine.log file the TriggerValue you are being triggered with. You will likely find there are a series of triggers happening and that will explain what is going on and then you need to build the macro to be robust against those multiple triggering events.

How is the file getting there in the first place - quite possibly the app placing it there is creating temporary files. And then you are renaming the file, and that is placing a new file there effectively, so the whole situation becomes confusing.

Find out exactly what is happening, and what trigger events you are getting and that will clarify what is going on. And then you also need to deal with avoiding issues of causing the macro to be re-triggered when you rename the file.


Just to add a "Poor Man's Solution"...

I get this a lot with LaunchAgents and "Watched Folders". A quick'n'dirty fix is to:

  • Save your screenshots to folder A, for which additions (only) trigger your action, but
  • Move/rename your screenshots to folder B, which triggers nothing

...rather than renaming them in place.

So you'd save your screenies to a "Temp Screenshots" folder and process them out to your "screenshots".

1 Like

There’s nothing “poor” about that! I think it’s actually the best solution since it is simple to implement and avoids having to think about things like semaphores and guard-flags.

Might as well throw another idea in...

Set the trigger for this macro to your usual Screenshot hotkey, and replace the path to your Screenshots folder in the green action. It will ask you for a name, and if you don't fancy giving it a custom one, you can just hit enter and it will name it with the date and time instead.

Screenshot - Name and Save.kmmacros (40 KB)

Macro screenshot