How to execute a Macro Group’s “Show/Hide Palette” by name?

Hi,

I’d like to trigger Show/Hide a Macro Group’s palette by specifying the Macro Group’s name, similar to “Execute a Macro by Name”.
ref:

Is there a way to do this?
For example, I want to type the name of a Macro Group and then have Keyboard Maestro show or hide that group’s palette.

What action or AppleScript should I use to achieve this?

Thanks in advance!

2 Likes

The KM way would be to "Switch/Case" on the entered text:

Also check out the "Prompt with List" Action's "friendly values" section for how to have "easy" names in the prompt with "self-documenting" names in the Switch/Case.

If you want to do it somewhat dynamically you can use AppleScript, which will be slower:

Script
set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set groupID to getvariable "Local_choice" instance inst
	
	do script "<dict>
		<key>DisplayToggle</key>
		<true/>
		<key>MacroActionType</key>
		<string>MacroGroupToggle</string>
		<key>MacroGroupUID</key>
		<string>" & groupID & "</string>
		<key>ToggleMode</key>
		<string>TogglePalette</string>
	</dict>"
end tell

For a truly dynamic solution you'll need a way to also generate the prompt list. That's not too difficult if the Editor is open:

tell application "Keyboard Maestro"
	set theList to {id, name} of every macro group
end tell
set promptList to ""

repeat with i from 1 to count of item 1 of theList
	set promptList to promptList & item i of item 1 of theList & "__" & item i of item 2 of theList & linefeed
end repeat

...which you could also filter by the Macro Groups' enabled property.

Otherwise you'll have to do something like parsing out Macro Group UIDs and names from the XML returned by:

tell application "Keyboard Maestro Engine"
	getmacros with asstring
end tell

Since my testing is limited (Mojave, KM 11.0.4), I hesitate to add this hack to @Nige's comprehensive rundown of strategies.

Among the macro names in Trigger Macro By Name, there are entries that end with: [Macro Group] These entries are, I believe, the ToggleMacroUID of hot key activated macro groups--those whose activation option is set to other than: Always Activat.e Selecting an entry like this toggles the macro group in accordance with its activation setting.

So you could set the initial search term of a Trigger Macro By Name action to:[Macro Group]+space.

I believe it shows all of the hot key activated macro groups.

The initial search term will be selected, so you'll have to type the right arrow key before you start typing the name of your paletted macro group.

Or as described in this post, before the Trigger By Name action, place a subroutine, set to run asynchronously, that types the right arrow for you:
Unselect Initial search text in Trigger Macro by Name - Questions & Suggestions - Keyboard Maestro Discourse

Image

(If you do go down the getmacros or gethotkeys route, it can be a bit slower--or faster if you have a lot of macros--than Trigger Macros By Name. It can also be technical and, depending on the implementation, involved. Shout if you give it a try.)

For now here's the Trigger By Name hack.

USAGE:

  1. Open Test Macro Group.
  2. Enable the group, the main macro and the subroutine macro.
  3. Run
  4. Select a macro group.
Image

Toggle Macro Group By Name + helper subroutine.kmmacros (3.9 KB)