JS Script Works from the Script Editor but Doesn't Work from Keyboard Maestro Using a Hotkey Trigger

Hello people,

First time posting here, KM user for very long time.
Posting because I'm facing issue which is completely unexplainable but it happens.

I'm working on a UI automation project with JXA.
I've written hundreds of scripts and never got this behaviour.
Well, the script is nothing special, accessing a checkbox and clicking it and it works absolutely fine from script editor.
I chose F3 key to trigger this script from Keyboard Maestro but it doesn't run the script.
I checked everything 20 times most probably and everything is fine. If I select the action JavaScript for automation and from it's menu I choose try script option, out of a sudden the script got executed.
But if I try to execute this script by pressing F3 key absolutely nothing happens.

I even tried running simple hello world script, tried some other keystrokes, putting it in different macros but from some reason I can't run this and few more scripts which are as simple as this one and all of them work from script editor.

To me it seems like Keyboard Maestro is preventing this from some reason but I have no idea why would it do something like that because hundreds of other scripts work perfectly fine, with any combination of keys.

Does any of you have some idea what can be cause of this? It's incredibly annoying.

Of course I also tried obvious stuff, restarting Keyboard Maestro, operating system, multiple times.
All of my previous macros work no matter whether they execute some js scripts or not, except these few I mentioned.

Thank you guys in advance

As always, if it's a response that you are hoping for, then you would need to:

  • show the script, and the macro in which you are embedding it,
  • explain what you hoped to see,
  • and report what you did see.

Hey @Lucky-Virtuoso,

Welcome to the forum!  :sunglasses:

What @ComplexPoint said – and try running the Interactive Help from the Keyboard Maestro Help menu.

-Chris

Hey guys

The script doesn't matter here. As I already said it perfectly works if I run it from script editor.
From some reason, whatever I put in the script it doesn't work.

For instance:
kme = Application("Keyboard Maestro Engine")
kme.setvariable("testVar", {to: "Hello World"})

This will be ran from script editor, but not from this macro, I've just tried it. Totally unexpected behaviour.

I tried Interactive help. Everything is fine, the macro is triggered, Java script for automation action doesn't show any errors.
In the interactive help, it's said that some apple's accessibility bug or whatever may block the action or something like that.
Obviously, I enabled both KM and KME under security and privacy, that shouldn't be the case I guess.

The weirdest thing here is that this is happening on few scripts only, from no reason. I have hundreds of other scripts and they all work fine, totally unexpected behaviour.

Even if I made some errors in script itself, it would then at least run the above example I wrote but it doesn't.

Unless you share the macro, I think you can enjoy a fairly high level of confidence that no useful response is feasible, or really even imaginable.

1 Like

Ok no problem

Will be home in about half hour. Should I send a screenshot or upload macro somewhere?

If you select the macro in the Keyboard Maestro editor, you will find that the menu system offers you File > Share > Keyboard Maestro forum

(or you can use File > Export > Export Macros and drag a .macros file into one of these forum edit fields)

Ok man.

Here's one of the macros which doesn't work, although script works normally from script editor. But as I mentioned earlier, even if it's hello world script, from some reason it doesn't work.

No reset.kmmacros (3.1 KB)

Thanks a lot

Do you have an example macro that doesn't depend upon an external library?

Everything is more the less depended on external libraries, including these few which don't work.
Give me a sec, I'll rewrite this so necessary functions are included in the script.

Ok here it is, everything in one script. I made sure it works, although it still doesn't work from keyboard maestro.

No reset.kmmacros (3.4 KB)

This works fine for me -- it must be something peculiar to your setup.

Starting with the basics -- what versions of macOS and KM are you running?

1 Like

Hmm intersting.

As I mentioned previously, it works for me either but if ran from Script Editor.

I'm on latest version of Monterey, MacBook pro 16 2019., Keyboard Maestro is also latest version (10.2).

I've just finished reinstalling Keyboard Maestro, imported all of my macros and still the same. Pretty pretty weird. Gonna try it on my friend's Mac tonight to see will it work on his machine.

This weird behaviour started on Tuesday. I didn't make any changes on my system in last 4 weeks or so.

It literally started behaving like that from nowhere.

I have no idea what else I could try.

AFAIK KM doesn't itself "run" the JavaScript -- it hands it off to osascript. Try the following in the Terminal, keeping an eye on the variable in KM's Preferences pane to see if it changes:

osascript -l JavaScript -e 'kme = Application("Keyboard Maestro Engine");kme.setvariable("Global__testVar",{to:"Goodbye World"})'
1 Like

Yeah it does change.

I firstly had to allow Terminal to use the Keyboard Maestro engine, but yeah it worked.

Have you tried creating a new, clean user on your own system?

No I haven't.
I'll do it immediately.

Thanks guys for suggestions. Keep you updated.

1 Like

Now give this a go, still in your usual user account:

JXA Test.kmmacros (4.1 KB)

Image

If that works you'll know KM/JXA/macOS is generally OK and it's something specific to the failing macro(s). If it doesn't the JXA action is set to throw up a "results" window, which might give you a clue.

Hey Nije_s

Yes it worked as expected. Tried multiple times with different keystrokes and things like that. Also I tried it in new user and it worked every time.

BTW, I created new user, firstly as standard user, then as administrator user. In both cases same behaviour.

Just tried on friend's mac, same model same OS and it works with no issues on his machine.