I'd like to get a list of macros in a given macro group without the KM Editor being activated.
The method I know of is to script the KM Editor. This will require the editor to be activated:
tell application "Keyboard Maestro"
tell its macro group "Macro Group Name"
set MacroIdList to id of every macro
set MacroNameList to name of every macro
end tell
end tell
I guess it's probably possible to get from the macro file. But I don't know how.
My ultimate purpose is to execute the macro that is
Thanks. JXA is over my head. I only learned some JS.
I can get the XML file in AppleScript from here:
But I don't know how to move forward.
I did some search and saw AppleScript can do XML search. But I have not made a successful attempt yet.
A regex search might also serve my need. It'll involve sed in shell script. I'm a novice to all of them. Dan's JXA example is excellent. I have not found a similar example in AppleScript.
I missed this in my first reply.
The macro by @DanThomas that I gave you does most of what you want.
If you append a text tag to the macros of interest that IDs the Macro Group, then you would have mostly what you want.
For Example, I use "@OL" to ID macros in my Outlook Group. Display Outlook Signatures @OL
So I can type "@OL" to list all macros in the Outlook Group, and then type some characters in the name of those macros.
You can get the macros without parsing the plist file:
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/03/09 23:32
# dMod: 2021/03/09 23:32
# Appl: Keyboard Maestro Engine, BBEdit
# Task: Send Macro XML to BBEdit
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Keyboard_Maestro_Engine, @Send, @Macro, @XML
--------------------------------------------------------
tell application "Keyboard Maestro Engine"
set macroList to getmacros with asstring
end tell
bbeditNewDoc(macroList, true)
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on bbeditNewDoc(_text, _activate)
tell application "BBEdit"
set newDoc to make new document with properties {text:_text, bounds:{0, 44, 1920, 1200}}
tell newDoc
select insertion point before its text
end tell
if _activate = true or _activate = 1 or _activate = "activate" then activate
end tell
end bbeditNewDoc
--------------------------------------------------------
And you can do sophisticated search and search/replace with AppleScript:
You can work the XML with AppleScriptObjC or a number of other tools.
So you should be able to do what you want, if you're willing to put in the skull-sweat.
Thanks a lot @ccstone. You keep recommending stuffs and I keep downloading them. Often I check my storage and/or clear the trash bin as my Mac is installing those stuffs. I know they are very light and probably won't have a noticeable effect to the hard drive storage, but I just can't help.
I was wondering about that too.
The extra handler only opens a new doc in BBEdit and puts the entire XML file there. But I surmise maybe that was all that Chris means by "get the macros without parsing the plist file".
Although, I think I might have a script somewhere that does it – no, not finding one. I thought that I'd done this with the shell, but I can't find it right now.
I actually found some posts parsing the XML with AppleScript, e.g.,
Also, there are posts about using Shell scripts for RegEx:
and using AppleScript Objective-C for RegEx:
But I was not able to twist my codes to get the desired result.
After a couple of hours, I had to quit. I can't understand the example that uses AppleScript Objective-C. I have not studied anything about Obj-C.
Thanks, @ComplexPoint.
I saw a similar JXA code in @DanThomas's macro referred to by @JMichaelTX in the first reply.
My question is: Is there a way to use run these JXA codes in AppleScript, to pass the JXA result to a variable in AppleScript?
Or, is there a similar code for AppleScript to do the same thing as the JXA codes do?
JavaScript is a handier instrument for this because the parse of the XML maps straight through to the JSON object structure.
AppleScript's problem here is not so much that it's relatively slow (which may not matter much) but that AppleScript records are a bit fragile and tricky to work with.
If you ask a JS object / dictionary for the value of a key which it doesn't have, it just returns the special value undefined.
If you do that to an AppleScript record, it trips an error.
This would be more manageable if we could easily ask an AppleScript record what keys it does have.
In JS you can do this directly with the built-in Object.keys() and Object.getOwnPropertyNames(),
but in AS that's not a native possibility, though there are some slightly more fiddly routes through the foreign function interface to NSFoundation etc.
The bottom line is that if you want to work with KM XML, using AppleScript may not really be the simplifying choice.
One approach, if you are invested in AppleScript, may be to:
Read the XML and extract values using Execute JS actions,
IMO, the only advantage that AppleScript has over JXA is the availability of a great script IDE and debugger: Script Debugger 7.
As I have said before, I much prefer JavaScript as a language over the language of AppleScript. So, if you are just now learning either AppleScript or JXA, I would suggest JXA. The Apple Script Editor, which is required for JXA, can use the Safari-based JavaScript debugger.
I learned a little bit AppleScript to do some simple jobs.
I have not learned anything about JXA yet. But I did learn some basic JavaScript to work with HTML. I'll try JXA in the future. Thanks for your recommendation!
The JavaScript core is the same for web (HTML) as it is for JXA (apps).
In one case you are working with the DOM (HTML Document Object Model), and in the other you are working with macOS app model.
See JXA Resources.