KM helps this hobbyist perk up an old Mac running Mojave, with a little required assembly and help from posts on this friendly forum and others.
I'm not sure if this is the right place for this, but here goes.
Here is an AppleScriptObj snippet that helps me get a bit more use out of KM's resources as well. Hopefully, I hope it can help on the task of getting a list of macros without activating the KM Editor and and then adding a "mg_uid" and "mg_name" key/value pair to hold each macro record's containing macro group's uid and name values.
This snippet uses a "keypath collection query" to pull an array of all macro records from the output of the getmacros command provided by the Keyboard Maestro Engine Applescript Suite. It should work with output from the gethotkeys command too.
(I hope it doesn't duplicate another post's wheel. Or, worse, once it leaves my machine, doesn't spin at all...)
Summoning the macros array: two compact--but exceedingly melodious--forms.
- string as input:
use framework "Foundation"
tell application id "com.stairways.keyboardmaestro.engine" to set pxml to getmacros with asstring
set macroDictionaries to (current application's NSString's stringWithString:pxml)'s propertyList()'s valueForKeyPath:"@unionOfArrays.macros"
- data as input (same result):
use framework "Foundation"
tell application id "com.stairways.keyboardmaestro.engine" to set asData to getmacros
set nsDataObj to (current application's NSArray's arrayWithObject:asData)'s firstObject()'s |data|()
set fObject to (current application's NSString's alloc()'s initWithData:nsDataObj encoding:(current application's NSUTF8StringEncoding))'s propertyList()
set macroDictionaries to fObject's valueForKeyPath:"@unionOfArrays.macros"
That hurdle cleared, we can add the "mg_uid" and "mg_name" key/value pair (to hold the uid and name of a containing macro group) to each macro record.
For example.
I have more than a few macros whose names contain the word "test" festooning various macro groups.
The following script shows one way to work with getmacros to:
First, to get an array of macros whose names contain the word "test".
Second, to add an "mg_uid" and an "mg_name" key/value pair to each macro dictionary, showing its containing macro group's id and name.
use framework "Foundation"
tell application id "com.stairways.keyboardmaestro.engine" to set pXML to getmacros with asstring
set mgDictionaries to (current application's NSString's stringWithString:pXML)'s propertyList()
set macroDictionaries to mgDictionaries's valueForKeyPath:"@unionOfArrays.macros"
set macroName to "test"
set testMacros to macroDictionaries's filteredArrayUsingPredicate:(current application's NSPredicate's predicateWithFormat:"%K CONTAINS %@" argumentArray:{"sort", macroName})
set testMacros to testMacros's mutableCopy()
repeat with i from 0 to (count of testMacros) - 1
set amacro to (testMacros's objectAtIndex:i)
set mUID to (amacro's objectForKey:"uid")
set predContainsUID to (current application's NSPredicate's predicateWithFormat:"%K CONTAINS %@" argumentArray:{"macros.uid", mUID})
set {anMgUID, anMgName} to ((mgDictionaries's filteredArrayUsingPredicate:predContainsUID)'s firstObject()'s objectsForKeys:{"uid", "name"} notFoundMarker:"not found")
set mDict to amacro's mutableCopy()
(mDict's addEntriesFromDictionary:{MG_UID:anMgUID, MG_Name:anMgName})
(testMacros's replaceObjectAtIndex:i withObject:mDict)
end repeat
return testMacros
The Keyboard Maestro Editor Suite, of course, makes all this a breeze.
What we want assembles promptly when KM Editor AppleScript issues this command:
tell application id "com.stairways.keyboardmaestro.editor" to set testMacros to macros whose name contains "test"
Each macro, now an object with its act together, answers the call to muster at a moment's notice, replete with info such as with its xml, its actions and their xml, in addition to its containing macro group and its properties and elements.
However, filtering with "keypath collection queries" and NSPredicate provides an alternative facility for coaxing similar results from getmacros for times when we don't need macro or action xml or the KM Editor.
Bonus: Fed a keypath collection query result, sorting with NSSortDescriptor is straightforward and quick.
Apple's page on: Key-Value Coding Using Collection Operators.
Various internet cheat sheets on NSPredicate help when one forgets the various incantations.