Using KM to create KM actions

I want to create a macro (using select menu item action) to produce the following KM actions in KM editor:

  • If then else
    • then: 2 pauses
    • Else: 2 pauses
  • 1 Pause (below the If then else)

Test - Engrouping - If statment .kmmacros (11.6 KB)

Note: I just used the pause actions for testing here, it will be replaced with something meaningful in a real scenario

This macro achieves the result above:

The red rectangle in the screenshot above is a keystroke action control + command + Right Arrow, which is a KM native shortcut for:

This should mean that if I replace the keystroke action control + command + Right Arrow with the Select menu item action (Enter Action), I should end up with the same result. However, It resulted in this (missing a pause outside of “If then else”) and an error message:

Here is the macro again, but replaced the keystroke action control + command + Right Arrow with the Select menu item action (Enter Action):

Why on earth did this happen???

If you are going to use KM to modify KM, which I advise against, you should at least be aware that this is a very difficult thing to do because the KM Editor has an autosave feature which can freeze the KM Editor for several seconds. This means that any fixed pause action, of which you have several, may suddenly not be long enough because of the time it takes the autosave to complete.

So if you really want to do this, you should modify your code to "wait" for the menu item to appear rather than assume that a 0.1 second pause is "always sufficient".

It's impossible for me to debug self-modifying code (my own or yours) because there's no way I can replicate your autosave timing. The timing of autosaves is unpredictable, inconsistent, and probably undocumented.

1 Like

Im feeling adventurous and rebellious! :smiling_face_with_horns: Maybe one day I’ll finally realize myself it's not a good idea to modify KM (hopefully not haha):sweat_smile:

Yup, that works! Wouldn’t that negate the auto-save issue then? And by extension, wouldn’t it work on any Mac with different autosaving timing?? (because it “waits” for the menu item to be enabled or disabled before moving on to the next action)

Btw, So far, I have some success with adding KM actions with KM and it seems to be ok reliable, LOOK:

CleanShot 2025-10-22 at 16.39.00

But who knows, it may crash later…

You now have more experience than I do at using KM to modify KM, so you can teach me henceforth.

1 Like

Nice joke :joy:

Asking a question that never gets old (for me -- maybe it does for you!) -- why?

There are reasons. Using your example: Perhaps this is part of a bigger macro that first "Prompt"s for a time, adds the actions, then changes all the "Pause" times to the number you entered in the "Prompt".

But if this is boilerplate that is either always the same or that you are going to change in the Editor anyway, "Favorites" is a far better way of doing this.

Obviously you know about Favorites, you're using them in this macro, but you may not have spotted that you can Favorite multiple actions as a single item (with or without Grouping them).

For the next level in automated macro-building -- Actions can be defined in XML, and if you load the System Clipboard with Action-defining XML then Paste into the Editor you'll add those Actions to the macro. Since that Paste is a single event you don't get the timing problems @Airy mentioned.

And since XML is just text it can be manipulated with other KM Actions, using prompts, conditions, variables etc to create the definition of the Action(s) to add to your macro. There's been a bunch of threads about this, either pasting in XML or even generating whole new macros with AppleScript, so have a search of the Forum for more.

1 Like

If you want to control Keyboard Maestro itself, like any good AppleScriptable application you should use AppleScript which will be far less fragile to minor UI changes.

Creating Keyboard Maestro actions should be relatively straight forward with AppleScript.

1 Like

It is part of a bigger macro I’m trying to make indeed. But not exactly what you said. Rather than dragging and dropping actions, I want to type out all the actions (including the settings of each action) at once. It’s almost like making my own programming language of sort (But much less sophisticated ofc). It’s a good way to practice the regex stuff I learnt recently and it’s help me make macros faster in the future, so two birds! :joy:

Yup Yup I came to know this when I started working on this little project of mine!

Oh great, you are hitting me with stuff I have never heard of again! I’ll look into this thing call XML! thx!

OK, applescript, got it! My list of things to learn is getting real long now! :smiling_face_with_tear:

2 Likes

Just ask the specific question and likely one of the AppleScript (or JXA) experts will help you write the script.

2 Likes

Start by looking at Peter's macro to create a text expansion macro from selected text -- not only a good demo of creating a macro using AS, but also a very useful macro in its own right.

2 Likes