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
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>"
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 (
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]]
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.
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.
Thanks for the macro! Pretty slick
Check out my results:
DURATION : using AppleScript
DURATION : using Edit>Copy as>Copy UUID (+ actions to retrieve from the Clipboard)
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
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
Build MacroGroups_dict.kmmacros (16 KB)
From there, I get the dictionary entry for the UUID based on the front application, like this:
Thanks, @rolian for the idea
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!