Thanks, Peter.
What about the single character prefix that @Tom and I suggested:
I dislike verbose variable names.
Thanks, Peter.
What about the single character prefix that @Tom and I suggested:
I dislike verbose variable names.
Yes, @peternlewis, if I look for example at the screenshots of my Retina Macros, which are exclusively using instance/local variables, honestly, I cannot say it is nice to have a five- or eight-letter prefix on each variable! It’s clunky.
So, this time, I completely agree with @JMichaelTX.
I have not decided on a choice for them, nor weighed the consequences of such a choice. So no, there is currently no short form.
This is resolved for the next version (after 8.0.2). Local and Instance variables will be in the environment variables.
Local Variables will work in Prompt For User Input and (non-asynchronous) Custom HTML Prompts.
As of 8.0.3, local and instance variables should work in Prompt For User Input and Custom HTML Input windows.
Also, you can now access them from scripts by using the KMINSTANCE environment variable as a key to which set of variables.
set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set v to getvariable "LocalVar" instance inst
setvariable "LocalOut" instance inst to "FromAS"
end tell
v
Wow! Many thanks for this, Peter.
This is huge.
I just confirmed it running Keyboard Maestro 8.0.3 (8.0.3) on macOS 10.11.6.
Also confirmed that running the same script outside of KM does NOT have access to the local/instance variable, which is proper.
To improve my understanding of how these variable types work, I wrote the below macro. Hopefully it will help others as well.
Everything appears to work as advertised!
Please feel free to ask any questions about this.
###MACRO: Local & Instance Variable Test KM 8.0.3
~~~ VER: 1.0 2017-10-04 ~~~
####DOWNLOAD:
Local & Instance Variable Test KM 8.0.3.kmmacros (15 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
###ReleaseNotes
Author.@JMichaelTX
PURPOSE:
REQUIRES:
NOTICE: This macro/script is for TEST Purposes ONLY
HOW TO USE:
MACRO SETUP
TAGS: @KM8 @LocalVar @InstanceVar @Script
This is great! Many thanks!
@peternlewis, thanks again for giving us local and instance variables. Very, very useful.
I know you're going to shoot me for asking this, but I need to ask:
Could you please provide a way of viewing local and instance variables while a macro is running?
One UI would be to allow them in the Pref > Variables panel.
Use Case: Would greatly aid in debugging a macro.
Without being able to see these variables as we step through the actions makes it very hard to identify the problem sometimes.
I'm sure you understand, since you have used many software debuggers over the years.
Actually it just hit me: Maybe a better UI would be to have a Variables panel in the KM Debugger, you know, like the Chrome JavaScript debugger:
Thanks for considering this request.
For now, the best solution is probably to use the Log action and tail -f
or Console to watch the Engine.log file.
What is the JXA equivalent for the above codes? Thank you.
I don’t know. Perhaps someone who is better versed at JXA can answer.
I finally worked it out:
var app = Application.currentApplication()
app.includeStandardAdditions = true
var kmInst = app.systemAttribute("KMINSTANCE");
var kmeApp = Application("Keyboard Maestro Engine")
var myLocalVar = kmeApp.getvariable("Local__MyVar", {instance: kmInst});
kmeApp.setvariable("Local__FromJXA", {instance: kmInst, to: "Set in JXA Script"})
Here's my simple test/demo macro:
###MACRO: Get Local Variables in JXA Script
~~~ VER: 1.0 2017-10-11 ~~~
####DOWNLOAD:
Get Local Variables in JXA Script.kmmacros (3.1 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
Great! It would take me forever to figure it out, which means I would not be able to. Thank you.
Not as good as watching variables update live but:
Here's a macro to display all current Instance variables. Easily modifiable to also show Global variables. Can't show any local variables (even ones defined in the macro).
I use it as a subroutine and just call it as needed while debugging.
Thanks for sharing.
That is proper behavior for local variables.
However, we can access local variables from AppleScript.
But I don’t know how to get a list of local variables like I can with global variables:
set kmVarList to every variable whose name starts with kmPrefix
@peternlewis, any way to make this work for Local variables?
OK, thanks Tom. If we can get them from a shell script, then we should be able to get them from AppleScript. One issue with the shell script is that how can we tell if the underscores between words in the KM var name are actual, or just for the shell?
Good catch. Running this…
…I only get this:
That is: the first variable (the one without the underscores) is not in the environment. But it should be, I think.