How To Use "VS Code" for JXA, with Keyboard Maestro

UPDATED 2019-09-10, around 09:30am PDT

CHANGE LOG

2019-09-10, around 09:30am PDT: I discovered how to no longer need the "shebang" in your script file.

2019-09-10, around 08:30am PDT: I found what I think is a much easier way to run JXA code from VSCode. So I've rewritten most of this post.

Visual Studio Code (VSCode)

I have been successfully using VSCode to compile and run JavaScript for Automation (JXA) for a while now. I thought I'd share what I've learned, and I'll answer any questions I can.

VSCode vs. Atom

As far as compiling and running JXA scripts, I think they're about the same. There's no debugging that I'm aware of in either of them.

As far as editing is concerned, VSCode is light-years ahead of Atom. VSCode gives you the full range of tools when working with JavaScript. Code completion, real-time syntax checking, Find All References, symbol renaming, Intellisense, region folding, the list goes on and on. I'm sure I'm forgetting some obvious things.

Installation

Download the Mac version from https://code.visualstudio.com/. If memory serves, you have to unzip it, then drag the app to the Applications folder.

Then (1) Click the Extensions button on the left, (2) Search for "Code Runner", and (3) Install it:

image

You'll probably also want to install the "AppleScript" extension, which also supports JXA (more on this later):

image

"Code Runner" vs. "AppleScript" extension

Both of these extensions add support for running JXA scripts. I'm going to explain what I think is the best way to use either of these.

Language Mode

I strongly recommend editing your JXA scripts using the "JavaScript" Language Mode. This gives you full editor support for the language, as I mentioned at the start.

So, whatever extension you use for your JXA files, make sure it's associated with "JavaScript". Open one of your JXA files, and in the bottom right (yellow arrow) make sure it says "JavaScript":

image

If it doesn't say "JavaScript", click whatever it says (yellow arrow above) to bring up this window:

image

...and change the association to JavaScript (yellow arrow).

You can now edit your JXA scripts with full editor support, as mentioned above.

Running JXA Scripts with the "Code Runner" extension

The easiest way to run your JXA scripts is with the "Code Runner" extension.

1) Edit your user settings:

  1. Press Shift+Command+P, type in "user settings", and "Open User Settings":
    image

  2. It's a standard JSON file. Yours might be empty. Add the lines shown in yellow, and optionally add the lines shown in green:

    "code-runner.executorMap": {
        "javascript": "/usr/bin/env osascript -l JavaScript",
    },
    "code-runner.clearPreviousOutput": true,
    "code-runner.saveFileBeforeRun": true,
    "code-runner.ignoreSelection": true
    

2) Use the "Code Runner" extension to run the code.

Here's some ways to do that:

  1. Press Shift+Command+P to bring up the command window, and type in "Run Code:
    image

    -or-

  2. Use the "Run Code" shortcut as shown above, Control+Option+N

Compiling and Running JXA Scripts using the "AppleScript" extension

If you installed the "AppleScript" extension, you can use it to save compiled versions of your script.

I still recommend editing your JXA scripts using the "JavaScript" Language Mode (see note below).

When you're ready to compile or run your script, do the following:

  1. Change the Language Mode to "JavaScript for Automation (JXA)".

  2. Follow the instructions in the "AppleScript" extension for how to compile.

NOTE:

When the Language Mode is set to "JavaScript for Automation (JXA)", you only get rudimentary editor support. You don't get all the cool features like code completion, etc.

That's why I prefer to work with the Language Mode set to "JavaScript", and either use "Code Runner" to run it, or switch to "JXA" and use the "AppleScript" extension when I want to compile it.

4 Likes

That's fantastic, Dan! :+1:

Thanks for doing all the work to make VSC work with JXA, and then for the great job in documenting it for others like me to use. I'll definitely use your system.

So, I take it that you could not make debugging of JXA work through Safari, like we can with Script Editor?

OK, maybe I missed your "later" comments, but here's my question:
.scpt is normally a compiled file, from Script Editor. I don't see how that would open in VSC, which is text only.
So do you mean that you used the Finder to associate .scpt with a text format?

I think I may have found one typo:

Did you mean change to "JXA"?

So does that mean application objects you have created using JXA don't provide any auto-completion? For example, if you:

var appFinder = Application("Finder");
var itemList = appFinder.selection();

then when you type appFinder., does it offer any autocompletion?

I generally do like to run my scripts in KM from a file. What will I need to change in your system to make that work?

Thanks, again.

Thanks, Jim! I was hoping someone would find it interesting, so it means a lot to me that you're looking into this. :smile:

NOTE: I'm withdrawing the rest post of this until I research something I just found. I'll post back later.

1 Like

I've rewritten my instructions. Let me know what you think.

Dan

1 Like

Updated again. You don't need a "shebang" in your JXA file, after all.

And in case I didn't make it clear, and I probably didn't, this means you can use VSCode with your script files with no problem.

And I'll bet that if you want to actually save the compiled script when using "Code Runner", @ccstone could figure out how to do it, because you just need to put the BASH command(s) in the user settings file.

I'm done bugging you on this issue. I hope. :stuck_out_tongue: