Questions about ProcessAppleScript in Custom HTML Prompt

KM v10 added ProcessAppleScript function, noting: executes the AppleScript and returns the result (v10.0+).

Are there advantages over window.KeyboardMaestro.Trigger( macro, value ) that runs AppleScript?

Also, what does returns the result mean? What result will be returned and how to get them?
If, as the wiki example shows, we are to 'tell app "Finder" to activate', then I guess there is nothing needed to be returned?


I think it means that it evaluates as an expression, to which you can bind a constant name.

Something analogous to:

const x = KeyboardMaestro.ProcessAppleScript("2 + 2")

For example, at the Console Prompt of a Custom HTML Prompt action:


Note, of course, that the return value will always be a string.

As I understand it, .Trigger is a statement rather than an expression, so it has no value that you can retain after it is evaluated – it just produces an effect in another namespace, somewhere out of sight.

1 Like

the return value will always be a string

In fact I think that you will probably need to ensure that any value you want to come back from the AppleScript into the JS name space has a text / string type:

1 Like

Thanks, @ComplexPoint.
That's very helpful. It shows indeed we can get some returned results.

I'm interested in this: execute a KM macro via AppleScript and return a result back to javascript in the HTML Prompt, is it possible?

Getting late here, but it looks to me as if you might be able to do that, as long as the HTML prompt is running asynchronously.

Presumably the Applescript would do something like do script " ... " and then read the result from a KM variable, returning that value.

but it's beginning to sound a bit Baroque, and I don't know whether it would be the best solution to your problem. (That degree of complexity seldom turns out to be the most solid and dependable path)

What's the context ? It sounds as if you are edging towards some kind of Electron app : -)


Currently, I'm working on something like this:

  1. type some search words in the input box in the HTML Prompt, hit Search.
  2. based on my input, the javascript creates a database query command
  3. javascript trigger a KM macro with the query string as parameter
  4. javascript waits until the query completes
  5. javascript reads the query result (saved into a KM variable), and shows it in the HTML Prompt window.

I already have two solutions for step 4:

  1. javascript uses a while loop, waiting until the query completes (i.e., KM variable value changes).
  2. to use the execute javascript in HTML Prompt window.

I'm ok with either of the two solutions above. Now, I'm just trying to explore if the new function in KM v10 can provide a third solution. I hope to learn something new as well, especially that ProcessAppleScript can do what javascript and window.KeyboardMaestro.Trigger('macro') combined cannot, or that ProcessAppleScript has more advantages.

Depending on the use, other advantages of ProcessAppleScript are performance and efficiency. I have a Custom Prompt HTML Window that call KeyboardMaestro.Trigger periodically every 500ms, and KM's CPU usage shootup to 70%. Replacing it with ProcessAppleScript reduce it to 20% and the performance is improved. That make me realize how efficient ProcessAppleScript is. It could be less overhead calling ProcessAppleScript than KeyboardMaestro.Trigger. Mileage may be different but worth to try if one do need such efficiency.

It doesn’t matter if the Custom HTML Prompt is running asynchronously or not - other macros can still be executed regardless.

Modern JavaScript does not like “waiting” - working around that was a real pain in the conversion to WKWebView.

But I did work around it, and you can use that fact using the KeyboardMaestro.ProcessAppleScript, to have it run the macro, wait for it to complete, and return the value. Probably the easiest will be something like:

window.KeyboardMaestro.SetVariable( "Search", searchstring );
window.KeyboardMaestro.ProcessAppleScript( "tell application \"Keyboard Maestro Engine\" to do script \"Macro Name or UUID\" " );
var result = window.KeyboardMaestro.GetVariable( "Result" );

You could read the variable result in the AppleScript and return it from ProcessAppleScript, but it doesn’t make it any easier or clearer.

Basically you are just using the ProcessAppleScript synchronous properties, together with the do script synchronous properties.

Note that you may need to use AppleScript’s facilities to turn off timing out waiting for the result if the macro will take a significant amount of time to run.


I might be misinterpreting the problem, but couldn't you have KM use the information passed to it from the HTML prompt, process that information in KM in whatever way necessary, use KM to write an HTML file, using a skeleton HTML file with KM variables inserted into the parts that vary, then have KM open that HTML file in the browser of your choice? I routinely have KM write HTML by inserting variables into skeleton HTML, then have KM open the HTML file so I can interact with it, having javascript in the page process my choices and sending the values back to KM for further processing. That further processing could include writing HTML (using the variables sent from the first HTML), then having KM open the new HTML page to use that new information for whatever purposes you like. There is more information here:

1 Like

Thanks, @peternlewis. This is very helpful!

I'm wondering if there is a bug to KeyboardMaestro.ProcessAppleScript. It does not work in my test macro.

Test ProcessAppleScript in HTML Prompt.kmmacros (3.7 KB)

I have it to trigger another macro (also included in the macro above), it does not trigger it.
window.KeyboardMaestro.Trigger('alert - display text briefly'); (commented out in the javascript) works.

The other macro contains only a simple action:

Thanks for the suggestion. But my whole purpose is to handle everything in the same HTML Prompt window.

I haven't read through all this, but I just had a situation where I couldn't trigger a macro from a Custom HTML Prompt.

It turns out the macro group was only enabled if Keyboard Maestro was the active application, and I guess it decided that when the prompt had the focus, Keyboard Maestro wasn't the active application.

I put the macro into another group that didn't have any application dependencies, and everything worked fine.

Hope this helps.

Thanks, Dan.
But in my case, both macros are in a global macro group that does not have any conditions set.
The only difference is that window.KeyboardMaestro.Trigger works while KeyboardMaestro.ProcessAppleScript does not.
I put the AppleScript in another AppleScript action in the same macro and it works.

1 Like

It looks like the ProcessAppleScript cannot be used to control Keyboard Maestro Engine itself. I'm not really sure why, the error message is not particularly helpful. It may be some sort of security restriction. It does not seem to affect the Execute an AppleScript action, so maybe it is related to whoever the WKWebView is communicating with the Keyboard Maestro Engine which somehow drops the permissions it is allowed.

I'll continue looking, but currently I have no leads on why this is failing.

1 Like