Enable/Disable Action within a Macro using Hotkey

I'm trying to enable or disable an action within a macro using a hotkey. Within the "0.5 Auto or Manual" macro, I have an action that I want to enable or disable using a hotkey, like a toggle. In some cases, I want this action to run and in other cases, I want it to be bypassed. So, I want use another macro's hotkey to enable to disable that macro's action.

I came across the attached applescript on this forum enabling/disabling an action, but am unsure of how it works. I'm guessing it selects the "Action" menu and scrolls down to the 4th item in the list. But I don't know what "menu bar 1" means. How does it know which macro and which action within the macro to affect?
Screenshot 2024-04-22 at 9.10.38 AM

0.5 Auto or Manual Macro (v11.0.2)

0.5 Auto or Manual.kmmacros (2.3 KB)

Keyboard Maestro Export

Hi, @Bret. Try this...

All it is doing is using UI scripting to select the "Enable/Disable Action" item in KM's "Actions" menu in your menu bar (that's the menu bar 1 bit). So it works just as it would if you did the same maually -- it enables (or disables) the action that's selected in the KM Editor.

But this really is for when working on a macro in the Editor. If you want to modify the macro's behaviour at execution time then use an "If Then Else" action and test for a condition -- my favourite is to have two triggers the same except one includes the ⌥ key, and if the %TriggerValue% contains ⌥ do A otherwise do B.

I actually had this from last week in my downloads folder. I've been trying to figure this out for a while and did come across that thread and macro, but couldn't figure out how to modify it, in order to select the specific macro group, specific macro, and specific action of the macro. Are you able to decipher it?

I tried replacing "Action" with the action name (which I renamed to make the reference name shorter) and replacing "Macro Group" with the group name of the macro. But it failed...really not sure how to utilize the macro.

Not sure I entirely follow, but I'll give it a go. Thank you.

The action you posted (and, I believe, the macro @_jims linked to) are for enabling/disabling actions while you are working in the KM Editor.

It sounds more like you want run-time "flow control" -- "when you get to this action, if condition A applies then do it, otherwise don't". That's when the "If Then Else" and "Switch/Case" statements come into their own.

Have a try, but if you get stuck then post an example macro and explain what you are trying to do (or not do!).

Not exactly. This is an action I want to toggle on or off before I run my macro. There are about a dozen macros that all tie in to one long process. This is just one step I want to skip entirely (or leave enabled), which I'll know before I ever run the macro.

Here are the basics:

  1. I hover my mouse over text...the macro pauses and waits for keyboard input
  2. after keyboard input, it automatically triple-clicks the text in order to copy it
  3. it gets assigned to a variable and pastes the text into a dialog box.
  4. rinse/repeat for up to 6 pieces of text that will fill out a dialog box.

Step 2 is what I want toggle on or off. In some instances, I only want to copy one word, which can be selected automatically. In other instances, I have a string of text that needs to be copied and this can't be automated to my knowledge, since the number of words I need to copy may be different. Therefore, I will manually select the text before continuing the macro.

I'll know before I ever run the macro if I need this whole process to copy single words (which can be selected automatically) OR if I need to copy phrases (which I need to manually select during the macro process).

Hopefully that makes sense.

Based on your usage description, instead of trying to disable a step (complicated), why not just use two triggers for the same macro, and then use an If-Then to skip (or not skip) the key step based on the trigger?

Sure, it's one minor thing to remember (which keys to use to activate the macro), but you said you always know before hand, so it shouldn't be too hard to get used to the two sets of keys.

Here's a simple proof of concept—the trigger is set to a key on my numeric keypad, as I'm running out of other keys to use :), so feel free to change it to whatever. Just make sure one has the Option key and one does not.

Download Macro(s): Act based on trigger.kmmacros (3.1 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

You'd then just wrap your step two in the If-Then clause, to skip it when you don't need it based on the keys used to launch the macro.

-rob.

Do you mean a dozen macros, or a dozen actions in one macro?

If it's the first then how you get your "adjustable" macro to behave the way you want will depend on how you are triggering it, but the usual way would be to pass a parameter in. Post examples of both the macro and the one that calls it so that we can see what's needed.

If it's the second, @griffman has given you one example. Here's another which shows not only the behaviour you want but also how you can "Pause" the macro until you manually select and Copy some text if that's the Option chosen:

Option to Manually Copy.kmmacros (6.3 KB)

Image

It'll pop a couple of notifications to show you which path is being taken and then a window to show you the resulting clipboard.

1 Like

Thank you for writing that. I'll give it a go when I get a break from work. I'll probably have to fall back on using a multi-key hotkey like you wrote (i.e. 1 for auto or Option+1 for manual, 2 or Option+2, etc.) if I can't figure out how to disable/enable an action. I was thinking more along the lines of hitting a toggle switch (F9 for example) and having it send a notification that we're in "auto mode" (action enabled) or "manual mode" (action disabled) so I know what mode I'm in.

To answer your question, there are a lot of macros that lead-in to other macros, partly because I'm not progamming-literate and don't know how to make more efficient macros, but also to compartmentalize each step of the process to make it easier to modify and build-upon later. Attached is a screenshot of the whole process, with a short HowTo guide I wrote when I first made the set of macros. That might explain what's happening better.

I'm essentially copying hex codes which are getting transferred to a dialog box for later usage. The REFERENCE name I'm using to find the hex codes is either a color number (which is a single word that can be tripled clicked automatically) or a color number that has a dash in it (in which case, triple clicking doesn't select the entire phrase, requiring manual selection). Attached is a design request and the reference names I copy (which you can see highlighted) in order to obtain their hex code in another document. [we have a website that does everything automatically, but it's down and we're relying on me to manually create all of the design requests in Photoshop].

Screenshot 2024-04-22 at 2.16.24 PM


That's certainly another way to do it -- use F9 to toggle the value of a global variable, then have your other macro choose which "branch" to take depending on the variable's value.

Option Toggler.kmmacros (2.8 KB)

Image

Option to Manually Copy (Toggle Version).kmmacros (6.1 KB)

Image

It is possible to do what you want, enabling/disabling an action then running the macro, though it certainly isn't the "accepted way" of doing this sort of thing! If we can't convince you to use flow control within the macro you can use AppleScript -- you'll have to get the macro and action ids first, by selecting the action in the KM Editor then running the following AppleScript:

tell application "Keyboard Maestro"
	return (get the selection)
end tell

...which will get you something like

{action id "15874111" of macro id "F7661F0D-9381-4FC4-BE21-BF2E0353E902" of macro group id "60FE925B-A085-41DC-B552-3299C9508222" of application "Keyboard Maestro"}

You can then use the ids (you don't need macro group id) in an "Execute AppleScript" action in a KM macro. Using those from above:

tell application "Keyboard Maestro"
	set theAction to action id "15874111" of macro id "F7661F0D-9381-4FC4-BE21-BF2E0353E902"
	set enabled of theAction to not (enabled of theAction)
	return enabled of theAction
end tell

You can then use the value returned in a dialog to show if the action is enabled or disabled.

But I must stress that this is less efficient than branching within a macro. Not only is there the expensive of unnecessarily running an AppleScript action, it will also launch Keyboard Maestro if it isn't already open

1 Like

This is exactly what I'm looking for :smiley:

I understand it's a dumb way to do things, but it's honestly the quickest way (or fewest amount of keystrokes) I can see to accomplish what I'm after. Thanks so much.

Is there a way to use the "enabled of the action" piece of the script, in an If/Then action, to pop a notification to say which state its in (Enabled saying "Auto" or Disabled saying "Manual")?

Yes -- as written the script returns true when the action is enabled, false when not. You can save that to a KM variable, test that and display what you want depending on the results -- or you can change the AppleScript to return a meaningful message and then display that result directly as part of the action.

You'll find the "Save..." and "Display..." options in the action -- it defaults to "Ignore results":

If you want to "Display results in a notification" directly in the action then you just need to change the script to return something meaningful:

tell application "Keyboard Maestro"
	set theAction to action id "15874836" of macro id "3A07A715-18FB-403A-A50F-B97DD4B5057C"
	set enabled of theAction to not (enabled of theAction)
	if enabled of theAction then
		return "The action is enabled!"
	else
		return "The action is disabled"
	end if
end tell
1 Like

Thanks so much for your help. This is what I was after to a T.