How Do I use Sub-Macros & Prevent One from Starting Until the Other has Finished

I had to reread again, though still not getting it to work as hoped, the documentation and this thread are super helpful!

I am trying to reference two other macros from a single macro. The problem is as noted above that they seem to run side by side each other and reference macro 2 gets executed while reference macro 1 is still running.

Perhaps I am not understanding the Semaphore unlock still or there is a bug. From the Wiki.

  • When the Semaphore is unlocked the next macro instance in the queue will execute in First In First Out (FIFO) order. Since the first Action is a Semaphore Lock , only one macro instance is executed at a time.

Only having a Semaphore unlock stops macro from executing.

Example: I have several macros that have the same actions in them so I put two reference macros in each of them so I don't have to change all of them. It looks something like this.

Run Macro A
Reference Macro 1
Macro A unique action
Reference Macro 2

Run Macro B
Reference Macro 1
Macro B unique action
Reference Macro 2

Run Macro C
Reference Macro 1
Macro C unique action
Reference Macro 2

etc.

Problem is Reference macro 2 runs before Reference Macro 1 has completed. I stuck a Semaphore Unlock at the end of Reference Macro 1 but then Macro A unique action doesn't run and the macro just stops. That doesn't make any sense to me at all. I have spent hours on this and tried this.

Run Macro A

  1. Semaphore Lock
  2. Reference Macro 1 - Semaphore Unlock at end
  3. Macro A unique action
  4. Reference Macro 2

Unfortunately Reference Macro 2 still runs at the same time as Reference Macro 1.

I tried this just to see what happened and was surprised to find that step 2 never runs because the Unlock stops it from running (seems like a bug or unusual behavior. Perhaps it freaks out and cancels all Macros because it didn't see any semaphore lock running with that name and so it is a hidden function @peternlewis built in.

  1. Reference Macro 1 - Semaphore Unlock at end
  2. Macro A unique action
  3. Reference Macro 2

Anyway, some help on how to get a single macro referencing other macros to run in order and not overlap would be greatly appreciated.

I need someway to tell Reference Macro 2 not to start until Reference Macro 1 has completed and Macro A unique action has completed. I need them to fire after each step has completed instead of Macro A firing all three actions as fast as possible.

It's hard to be sure without seeing the macros in question, and I suggest you upload them if this doesn't help with your issue, but do you have the "execute asynchronously" option turned on for the reference macros' Execute a Macro actions? Because my understanding is that the Execute a Macro action is supposed to work how you want by default, without the need for semaphores, in that the macros referenced should complete before the macro proceeds, unless that option is turned on. From the wiki:

The Execute Macro action allows you to execute another macro like a subroutine. The sub-macro will be executed, and when it is finished, execution will continue on from after this action.
You can optionally configure the action to be Asynchronous , which means the sub-macro will be executed, and the current macro will continue on without waiting so that both macros will be executing at the same time.

Thanks, I was just getting on to also say I don't have Asynchronous enabled. Thank you for your reply. Here is an example macro.

Quicken for Mac Macros (v9.0.5)

Quicken for Mac Macros.kmmacros (58 KB)

Thanks, that made it much easier to test. Unfortunately(?), I'm not seeing the issues you describe, so I can't say what might be going wrong for you. I don't have Quicken, so I'm not sure exactly what it is you're trying to accomplish with these macros, but since they only consisted of keystrokes and pauses, I tried testing them in BBEdit after modifying them to display notifications and insert text indicating when one reference began and ended, and it always seemed to complete in order:

These are the versions I modified in case you want to test for yourself, though I really didn't change anything besides the app where these macros are available and adding the new Insert Text and Notification actions:

Quicken for Mac Macros.kmmacros (61.1 KB)

I moved your new question, and all replies, to a new topic, since it really is a new subject.

Lets clear up some basics.

Keyboard Maestro can execute macros simultaneously.

Most actions are synchronous - they perform their task and do not progress until it is completed. However there are some actions (like the “Activate …” actions) whose task is simply to start something, and so the macro will continue running the next action after starting whatever task is requested. Also, many UI actions like clicking or typing or pasting involve the event queue, and thus the the target application may take an arbitrary amount of time to process the event - Keyboard Maestro waits a short amount of time, but it may not be enough. Further, some actions induce the target application to perform some task that takes time (like opening a window or sheet), and the action does not wait around for that to complete. You may need to use Pause or Pause Until actions in some of these situations.
If a macro is triggered, then it executes that macro, and if, while it is still running, another macro (or the same macro) is triggered, that macro will execute as well, so both will be executing simultaneously.

However, if you execute a sub-macro using the Execute a Macro action, that sub-macro will run to completion before the parent macro will move on to the next step (exception: if the action is configured as asynchronous, then the sub-macro will start executing and the parent macro will continue on to the next action and both macros will continue executing simultaneously).

So to execute two sub-macros in succession, you simply use two Execute a Macro actions in sequence. No semaphore lock is required.

What?

What does that mean? Clarity is required for this - is this an Execute a Macro action? If so, in your examples, Macro 1 will run to completion, then the unique action will run, the Macro 2 will run to completion.

This is incorrect. Whatever is happening, this is not it. Unless you mean something other than Execute a Macro for “Reference”, or unless the Execute a Macro action is configured as asynchronous.

It is certainly possible that the consequences of whatever Macro 1 is doing have not had time to complete before Macro 2 starts.

None of this is how Semaphores work.

Keyboard Maestro will wait at a Semaphore Lock action until no other running macros hold that semaphore locked. Then it will lock it and progress to the next action, and the lock will be released whenever the macro completes (including any parent macros), or whenever you use the Semaphore Unlock action explicitly.

None of this is the case, and I'm afraid I have no idea what you are talking about.

If the Semaphore Lock and Semaphore Unlock are using the same name, then the sequence:

  1. Semaphore Lock
  2. Reference Macro 1 - Semaphore Unlock at end
  3. Macro A unique action
  4. Reference Macro 2

will first lock the named semaphore (assuming no other running macros have a lock currently with that name. Then it will run macro 1 (assuming by “Reference” you mean a synchronous “Execute a Macro”). At the end of that it will unlock the semaphore, and then run the unique action and then run Macro 2.

If Macro B used the same named semaphore, and if it was triggered while Macro A was running, then it would wait until the Semaphore Unlock was performed at the end of Macro 1, and then it would start running.

I don't know whether any of that clears up anything, since I really don't understand much of what you are saying.

Thank you that was very helpful and thanks for the animated gif. Looks like maybe Quicken is just freaking out or something. I put in very long pauses and it doesn't even seem to help like 5 seconds and it waits and still glitches out. I will keep playing to see what I can figure out. I like that idea of using a text document to test this out, that makes a lot of sense. I still need to learn how to better use the Keyboard Maestro Debugger. I have never mastered that one.

Thanks.

Yes, Peter that helps a lot and clears up a lot at least of how I thought Keyboard Maestro was supose to work. I have a lot of referenced macros and not had an issue but I thought perhaps it was just dumb luck of sequence order the application received the macros.

Here is the macro for TextEdit thanks to @gglick. You can see I put a semaphore unlock and it stops the rest of the macro from running even though there is no Semaphore lock that I ran. Seems like odd behavior but maybe I misunderstood what you are explaining and that is normal.

Quicken for Mac Macros (v9.0.5)

Quicken for Mac Macros.kmmacros (61 KB)

With Semaphore Unlock on Reference Macro 1

The Semaphore Unlock in your macro stops the macro because it is trying to unlock the semaphore named “Semaphore Name” which you have not locked, so it fails and aborts the macro.

It reports this in a notification:

Semaphore Unlock “Semaphore Name” failed on unlocked semaphore

as well as in the log.

Even if the earlier Semaphore Lock action was enabled (which it is not), it uses a semaphore named “Quicken Categories”, so the unlock would still fail and abort the macro.

And in any event, unless you trigger this macro twice (by pressing the hot key a second time before the first one had finished), a Semaphore Lock will not accomplish anything.

1 Like

Thank you that makes so much sense, I completely should have figured that out!

I searched the menu for the log files is that something that is recorded by default in Keyboard Maestro.

Has this been added somewhere?

Thank you both this has cleared up a lot of things for me and helped at least confirm the way things are suppose to work. I'll keep plugging away at Quicken 2020 (5.16.1) for Mac and see what is causing the issues.

1 Like

You can see the log files with the Open Logs Folder in the Help menu. It keeps a log of every executed macro, as well as the failures among other things.