MacroNameForUUID but reverse?

I have a macro that, based on the front application, builds the xml to show a corresponding Macro Group. I'm getting the ID of the Macro Group like this:

tell application "Keyboard Maestro"
	set MacroUUIDForName to id of macro group FrontApp
end tell

And then the xml looks like this (which I based on copying the xml of an existing action and plugged in the variable in place of the UUID):

tell application "Keyboard Maestro Engine"
	do script "<dict><key>DisplayToggle</key><true/><key>IsActive</key><true/><key>IsDisclosed</key><true/><key>MacroActionType</key><string>MacroGroupToggle</string><key>MacroGroupUID</key><string>" & MacroUUIDForName & "</string><key>ToggleMode</key><string>OnceWithPalette</string></dict>"
end tell

This works fine, but finding the Macro Group ID doubles the run time of this otherwise snappy macro as compared to hardcoding it. I couldn't figure out a way to get the ID natively, so I was wondering if there's a more efficient way to get the ID from the group name, like a MacroNameForUUID token in reverse (MacroUUIDForName?).

It could be that token like MacroUUIDForName didn't exist because macro group name is not unique, meaning macro groups can have same name, whereas UUID is always unique.

You can make use of the following two tokens though if that work for you
[token:ExecutingThisMacroGroup [Keyboard Maestro Wiki]]

1 Like

Great point! That would be a dealbreaker.

Thanks for suggestion, but those tokens won't work in this case since the macro looks for the group that matches the front application, not the group the macro itself lives in.

Hi @evanfuchs.

As @macdevign_mac indicated, there are the tokens for the executing macro's group: %ExecutingMacroGroupUUID%, etc.

For other groups, it's not as easy. You can get the selected Group Name using Edit>Copy as>Copy UUID. Even though this method requires six actions, my testing indicates that it is faster.

I'd be curious to know if this is related to the specific script or if it is more due to the fixed overhead required when the Execute AppleScript action is used.

DOWNLOAD Macro File:
Get Selected Group UUID (AppeScipt vs Edit>Copy as>Copy UUID).kmmacros (16 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.


1 Like

Hey, @_jims.

Thanks for the macro! Pretty slick :slight_smile:

Check out my results:

DURATION : using AppleScript 
122.962 millisec

DURATION : using Edit>Copy as>Copy UUID (+ actions to retrieve from the Clipboard)   
22.98899996 millisec

My situation is a little different in that I know the naming convention of the targeted macro groups, which are named based on the application they apply to - App - Safari (menu) or App - Preview (menu) - so I can set the group name as a variable on the fly using the %Application% token:

KM grabs that variable in AppleScript and does the xml, which takes .25 seconds(ish) not counting the above-mentioned bit that gets the UUID from the group name. That itself roughly doubles the run time. Not a huge problem, but noticeable since it takes twice as long.

1 Like

Just spitballing here - I haven't tried this in KBM, but have done something similar in DEVONthink:

  1. Run a macro that collects all the UUID's for each macro - I am pretty sure Jmichael wrote such a macro but I am not in a place where I can look for it now. This is a one-time thing (or periodic as you add more macros)
  2. Massage the output of the UUID:Macro pairs into a dictionary, taking care to set the name as the key and the UUID as the value
  3. Use the dictionary to extract the UUIDs when needed rather than an Applescript that works on the fly. The dictionary lookup is quite fast, even with large dictionaries.
1 Like

Thanks, @rolian.

Absolutely. The thought crossed my mind before (naively) imagining a token to do the job. I have a few macros I've borrowed and/reworked that grab and parse the group lists. I will take a look and see what I can learn.

I've just been dabbling with dictionaries so this might be a good exercise. I suppose I could also automate updating the dictionary on a schedule :thinking:


Here is the dictionary I created, which gets the full list of macro groups and creates an entry for each group with its name and corresponding UUID. I'm not sure if I used the best approach to get and parse the list, using AppleScript and Search/Replace, but it works. I thought I'd share it here in case anyone finds it useful. And of course I am open to coaching :slight_smile:

Build MacroGroups_dict.kmmacros (16 KB)

Macro Screenshot

From there, I get the dictionary entry for the UUID based on the front application, like this:

Thanks, @rolian for the idea :+1:

Also, shout out to @DanThomas for both Variable Inspector Prompt and Dictionary Editor and Quick Access Macro Editor Palette, which made quick work of reviewing the dictionary and jumping back and forth between macros while I figured it out. Those really helped me learn as I went. Thanks!

1 Like