This macro builds on technique first demoed by @Nig_S that allows a KMEngine "do script" command to run an action's XML and get the return value. This works by appending a Return action's XML the main XML.
The macro gets the all of the variable/value pairs in the main XML of actions and retuns them dynamically as a JSON dictionary. This may be consumed by either JXA or AppleScriptObjC.
The trick is to get names of the macro's variables from the %LastAccessed% token. Then build a JSON dictionary with variable names and values as the key-value pairs. To do this, we run each of accessed variable names through the process tokens Filter, a Quote for JSON Filter, and keep adding adding names and variables to a JSONSetValue dictionary variable.
Then, like @Nige_S's script, set text field of a Return action to the variable containing the on-the-fly generated dictionary.
A do script running the main XML, plus the conversion XML will receive the JSON dictionary, which it can parse or deserialize.
- "Return Accessed Variables as JSON dictionary":
The first macro in the upload consists of the actions that generate the JSON dictionary. This group of actions can be copied and pasted to the end of any macro to output JSON dictionary containing all of the variables and values accessed up to that point as a JSON dictionary. It displays a simple Prompt For User Input and then packages up the prompt's variables as JSON for a Display in Window.
- "Consume JSON dictionary macro output with Do Script":
The second macro demos an AppleScript that consumes this output. It consists of the same Prompt For Input, which is and should remain disabled. The AppleScript imports the XML of the disabled Prompt, appends to it the xml of the group of conversion actions, runs the new prompt xml and returns the values of the variables in a Display in Window action.
Note that AppleScript runs inside the main macro, but that the Prompt For User Input runs inside the AppleScript as an independent macro that has the ability to return variable values as JSON.
Why do this? Why not?
The second macro shows how AppleScript can generate a dynamic Prompt For User Input when it appends the xml of actions to convert key-values to JSON . It might also generate a form displaying a set of global variables, pre-filled with their existing values. Do Script would then get user changes and make those changes. It might instead even make direct changes to the xml of Set Variable actions that were marked by color.
This technique's reliance on plist conversion and manipulation makes it onerous and prone to changes by Keyboard Maestro itself in how it stores macro info.
At any rate, it is, at least to me, kind of fun.
I hope others find some use for it.
Return values as JSON dictionary for do script.kmmacros (20.2 KB)