Insert a Macro Midway in Another Macro

I am not quite sure what to even look up for this question and I will do my best to describe what I am trying to do without being verbose.

I have several macros that have only one small change between them in the middle of several actions. In the past I have just duplicated all of those macros and hoped they still worked with software updates. I have many macros that reference other Keyboard Maestro macros through using AppleScript if the difference in the action is only at the start or at the end.

My delema is that I would like to have one reference macro that several other macros reference (so I can only update one macro should I need to) but the action in the other macros that is different is right in the middle of the referenced macro. I could make two referenced macros but that would get a little messy.

Is the best way to do this with a semaphore lock or is this something I would use a variable for since two macros are involved but actions still need to run in sequence?

  1. “Macro A” triggers first half of “Reference Macro X”
  2. Then triggers unique action in “Macro A”
  3. Then second half of “Reference Macro X” runs

This way

  1. “Macro B” triggers first half of “Reference Macro X”

  2. Then triggers unique action in “Macro B”

  3. Then second half of “Reference Macro X” runs

  4. “Macro C” triggers first half of “Reference Macro X”

  5. Then triggers unique action in “Macro C”

  6. Then second half of “Reference Macro X” runs

Etc.

@skillet, this can be done using sub-macros, using the “Execute Macro” action.

So, it sounds like you have one primary work-flow, with several options in the middle.
In that case, my approach would be:

  1. Create a sub-macro for each option.
  2. Create one master macro, with either a trigger for each option, or a popup asking the user which option to use
  3. Do your common processing in this master macro, and then based on the trigger or option selected, execute a different sub-macro at the appropriate step.

The Execute Macro action supports passing a parameter entered into the action. So you could pass any needed data like that.

If you search the forum on “sub-macro” or “submacro”, you should find several hits to give you an example.

Let us know if you have any questions. Several of us here use sub-marcros extensively.

Wonderful thank you, I was really struggling with the semaphore lock thing and couldn’t get that to work. I search for sub-macro and submacro.

Here’s another option.

  • Put the first half of “Reference Macro X” in one macro. Let’s call it “Reference Macro X part 1”.

  • Put the second half of “Reference Macro X” in another macro, and call it “Reference Macro X part 2”.

“Macro A” would look like this:

  1. “Execute a Macro” executing “Reference Macro X part 1”.

  2. Do the special action(s).

  3. “Execute a Macro” executing “Reference Macro part 2”.

“Macro B” would be the same, except for step #2. The same for “Macro C”, etc.