I'm working on dynamically building a "Prompt for User Input" action in JXA. I have almost everything working.
Consider this action:
I dynamically built that action with my JXA code, and it works fine. The XML I produce matches the XML from the real action, and "doScript" produces the correct prompt:
(If you're confused by the image of the prompt, KM removes the "Instance_" from the front when displaying the prompt, which is why it only says "var1:" instead of "Instance_var1:")
If I modify the value for variable "zz1" (which is a global variable), the variable reflects the change.
But if I modify the value for variable "Instance_var1", it does not actually modify the variable, and that's my question.
I would chalk it up to "doScript" not having access to Instance variables, and I suppose that's possible. But consider this:
Farbeit for me to think that I could ever correct you, but I'm a fool, so I will try.
That isn't correct. Tokens are not instance or local. It's better to think of them as "Global Functions" in my opinion.
The %Result Button% token acts more like a global variable, and maintains its value even after all macros have ended. So that proves it's not working like an "instance variable." Try it and see.
Clearly some tokens act like "instance" variables, for example %ExecutingInstanceName%, but even then, they still aren't the same thing as Instance variables. They are implemented in an entirely different way.
You can effectively return a number of values from.doScript (in the form of a JSON Array or Dictionary, parseable with the %JSONValue% token, or, of course, with JSON.parse),
but I think it invokes a separate thread, with its own evaluation space and distinct instance signature.
.processTokens and .doScript both return values to the calling evaluation space.
(in the case of .doScript, the XML needs to contain a Return Result action)
Compared with a "main" calling a sub-routine, where the instances are:
Main Inst: FD37EA36-C64D-409D-B287-640BBA37D65B:AAAD6BD1-1830-41F4-A488-6BD236B6ECCC
Sub Inst: FD37EA36-C64D-409D-B287-640BBA37D65B:3E4FB4DE-0F21-4F71-9C0C-B07BE6F8A097
Note the identical values before the :
Playing last night, I found "easy mode" to be:
In the KM Editor
Add a "Group" action
In that "Group", add a "Prompt for user input" action
In the "Group", after the "Prompt", add a "Return" action
Set the "Prompt" and "Return" actions' fields etc to "obvious" things so you can spot parts in the XML
Right-click the "Group" and "Copy as XML"
You can then hack around in your favourite text editor to change the XML as needed -- starting by deleting everything before and after the highest-level <dict> (basically deleting the first 4 and last 2 lines of the XML).
Your "new" action (using AS coz I still don't JXA) will then be
set theXML to "your XML from above goes here"
tell application "Keyboard Maestro Engine"
return (do script theXML)
end
...with the action set to "Save to variable" -- you can the use the contents of that variable later in the macro.
I like the way you’ve nicely summarized the steps, @Nige_S.
Like others on the forum, I use this approach often within macros to dynamically create Keyboard Maestro actions. (So often that I have a utility macro that does the top and bottom trimming that you mention.)