Run AppleScript With Specified Parameters

Run AppleScript With Specified Parameters.zip (50.3 KB) [ UPDATED: 2018-09-02 ]


Changes

Fixed an issue where paths to the script file were incorrectly processed if they contained characters that have special meaning in bash without first being properly escaped.

Under the hood, some code refactoring has, in theory, made the script faster to execute, whilst also shortening the script syntactically.

1 Like

@CJK, do you know if it would be possible to create a similar plugin, but for JXA scripts instead of AppleScripts?

I just did a quick test and I don't see any reason why it wouldn't be possible. I created a small JXA script:

function run(input) {
	var app=Application.currentApplication()
	app.includeStandardAdditions = true
	return app.displayNotification(input[0], {withTitle: input[1]})
}

Then I ran it from an AppleScript:

set message to "Success!"
set title to "Hello, World"

run script "/Users/CK/Documents/Scripts/AppleScript/scripts/jxa.scpt" with parameters {message, title} in "JavaScript"

The script executed correctly:

The only issue, which as far as I can tell is a minor one, is that the script returns an error -4960, about which there appears to be no information online:

This can be handled easily enough:

try
	run script "/Users/CK/Documents/Scripts/AppleScript/scripts/jxa.scpt" with parameters {message, title} in "JavaScript"
on error E number N
	if N = -4960 then return
end try

However, I can't find a way as yet to obtain a return value from the JXA script through conventional means.

In fact, given this, my plugin ought to work for JXA scripts too:

JXA.scpt is the same JavaScript script as above, and it executed correctly:

However, as I stated, there's no return value (although there's also no error -4960 from Keyboard Maestro).

Finally, I also just tested a quick mirror-version of my plugin where main.scpt (the script that runs within the plugin itself, behind the scenes) is written in JXA rather than AppleScript. Early signs are encouraging, meaning you can create the entire plugin using only JXA, without having to rely on AppleScript at all.

@JMichaelTX, apologies for the third message relating to this query, but upon further experimentation, it appears that using osascript to execute the JXA script is successful, without error -4960, and provides a return value.

Therefore, the plugin could make use of do shell script to execute the JXA with parameters, however this would, of course, mean a shell process is running for the duration of the script, which is less than ideal.

However, I suspect AppleScriptObjC's OSAKit would probably be the ultimate solution. My next iteration of this plugin was intending to convert the entire thing to ASObjC anyway, so I will make sure to test it with JXA scripts too.

No apologies are needed. Many thanks for all of your experimentation.
I agree with you that it seems like JXA should also work in this fashion.
Unfortunately, I'm consumed in a major project, so I've no time to experiment, for now.
But, "I'll be back". :wink:

Great tip. Made the macro availiableKeyboard Maestro Plugin Update.kmmacros (2.0 KB)