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.
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.
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.
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.
Just spitballing here - I haven't tried this in KBM, but have done something similar in DEVONthink:
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)
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
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.
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
EDIT:
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