JXA - How to Call a Periodic Function Before HTML Prompt Display

Posting a simplified version of the original question:

I am trying to write a JXA script to simultaneously do the following

  1. Display an HTML prompt because KM enables it very well (and @JMichaelTX has shown how to leverage it)

  2. Launch another function / script that at the same time as the HTML prompt is visible.

I learnt that JXA does not have setInterval and clearInterval and so could not figure out how to do asynchronous function launch.


For now, plan is to use multiple KM macros and enable / disable macro to run 1 and 2 simultaneously (Thank you @DanThomas).

At some point will try to figure how to do it in JXA - at first glance it appears run-jxa package can help.

I'll admit I only skimmed this, but here's a question: Why do you need a JXA script to do this periodic timer thing? KM already allows you to trigger a macro on a timer. You can enable and disable the macro as needed.

1 Like

@DanThomas Perhaps I should try to simplify my initial post. I will try to do so. Thank you for reviewing it and your reply!

I have been thinking about doing most of it in JXA and javascript and that was the reason. But your idea of enabling and disabling a macro is powerful and simple. And I might try that as an immediate fix.

I understand the thought of doing it all in JXA, I've tried that before. But I've learned to use KM as much as possible, and only resort to JXA when it is necessary, or will speed things up noticeably. With that said, I have a ton of JXA scripts. :grinning_face_with_smiling_eyes:

By the way, the reason setInterval and clearInterval aren't available in JXA is because they're actually a method of the Window object, which is the context used in HTMLs javascript. Since you're not running in a browser, you don't have access to it.

However, if you were using it in a Custom HTML Prompt, then you'd have access to it. If you want to call KM macros from within the custom HTML prompt, see the Wiki Entry.

Also, as far as I know, you can't use Node with a KM Script action. (I think you mentioned that in your withdrawn post, but I could be wrong.)

Thank you for explaining how setInterval etc is not available because it is in the context Window object in a browser. Need to learn javascript etc somewhat more.

As for why the tendency to do more in JXA than in KM, it just feels like if I could do more in JXA and more in script, then making changes will be easier than making changes in KM. KM is very powerful of course.

So glad I found KM and this forum, and thank you again to you and others here who help so gladly. Have a wonderful day!

1 Like