Newbie Help creating a Plugin Action with JXA?


#1

I have macro that I’d like to convert to a Plugin Action. I learn best by example, and I was hoping someone could show me how to convert this macro. Once I learn, I’m sure I’ll be able to develop some Actions other people could use.

The Action would be called “Set Variable to Value of Named Variable”. It would look pretty-much like “Set Variable to Calculation”.


Set variable [ ]
to value of variable named [ ]


Here’s the macro I want to convert. It’s quite simple. Right now, it returns the result in a variable named “GVVN_Result”:

Get Value of Variable by Name.kmmacros (4.2 KB)

And here’s an example macro to test it:

Appreciate any help I can get. Thanks!


#2

I got this working using AppleScript:

But I’d still like to see this in JXA, or if that’s not supported in Plugins, then plain JavaScript.

Thanks!


#3

Here is a version of your action with a JXA script. You just need to use a shell script with osascript -l JavaScript to execute it:

#!/usr/bin/osascript -l JavaScript

var kme = Application("Keyboard Maestro Engine");
var varName = ObjC.unwrap($.NSProcessInfo.processInfo.environment.objectForKey("KMPARAM_Variable_name"))
var varValue = kme.getvariable( varName );
varValue;

Get the Value of a Named Variable.zip (5.5 KB)


#4

Thanks!

How does this work, exactly - the different types of scripting languages that can be used? Do you assume it’s AppleScript unless it says something different? Or do you even need to know - do you just pass this script to some sort of shell?

Also, do you think there’s a speed difference between JXA and AppleScript?

And pardon my ignorance, but the first line says “JavaScript” - does that automatically mean JXA? God I hate being a newbie developer.


#5

I did some timing, and the AppleScript version is almost twice as fast. Interesting.


Help. Creating first plugin
#6

If the script ends with an AppleScript extension (like .scpt) then it will be passed to osascript.

Otherwise if it is executable, it will be passed to the shell to execute.

Note that you can also just save a JXA script as a .scpt and that will work as well because .scpt files include the language. So another possibility would be this .scpt:

var kme = Application("Keyboard Maestro Engine");
var varName = ObjC.unwrap($.NSProcessInfo.processInfo.environment.objectForKey("KMPARAM_Variable_name"))
var varValue = kme.getvariable( varName );
varValue;

Saved as a script called Action.scpt from AppleScript and referenced in your plist.

You might like to try that too.

Yes, it is osascript’s selection of language, either AppleScript or JavaScript (or potentially others, there have in fact been others in the past).

The AppleScript .scpt is pre-compiled.

The JavaScript .scpt or in the .sh is non pre-compiled.

That might make a difference. But this is a relatively trivial script, so all the time will be in the startup.

Although note that the AppleScript gets the variable name via system attribute, which will not work properly for non-ASCII variable names, where the JXA I wrote gets it from the environment dictionary, which may be unicode-safe (I’m not sure).


#7

Awesome - thanks for all the explanations. I might just learn something yet!