node-jxa icon indicating copy to clipboard operation
node-jxa copied to clipboard

How Do I use Visual Studio Code (VSC) with JXA and Retain JavaScript Code Completion?

Open JMichaelTX opened this issue 7 years ago • 4 comments

First, let me express my tremendous gratitude to John for providing this excellent library/resource. I look forward to using it.

Now to my question:

How Do I use Visual Studio Code (VSC) with JXA and Retain JavaScript Code Completion?

It is great that this extension provides us with the ability to code, compile, and run JXA files:<BR> AppleScript & JXA for Visual Studio Code

Unfortunately, JavaScript code completion is not active for a JXA file, even though JXA uses core JavaScript. Is there some way to enable it for JXA?

I am brand new to VSC, so if you can, please provide detailed instructions.

I realize this is NOT directly related to this repository, but I'm hoping the expertise is here that can answer this question. I've asked elsewhere without finding a solution.

Thanks.

JMichaelTX avatar Apr 19 '18 00:04 JMichaelTX

Hey @JMichaelTX,

No worries I love to discuss this stuff. 👍

I'm guessing that it works properly when you force it to use a Javascript syntax in the language selector in the lower right.

I use the idleberg extension too, it's awesome. But I don't use it for JXA anymore - these days I just use it for Applescript. If I'm understanding it right, it's providing two things for JXA files ( recognized by .jxa, .jxainc, or .js extensions ):

  • a JXA language syntax ( for syntax coloring ) that is based on Javascript's ( and there is nothing more for it to add ).
  • a set of commands for running, compiling and bundling JXA scripts ( with toolbar button(s) )

You can get both of these, plus the missing JS language support, by treating your JXA scripts as regular Javascript:

  • name your jxa scripts with the .js extension ( not .jxa )
  • include a shebang at the top; one of:
    • #!/usr/bin/env node-jxa # (if you're using it)
    • #!/usr/bin/env osascript -l JavaScript
  • use the Code Runner extension to run your JXA scripts - it regards shebangs by default.

The Code Runner extension actually includes support for running AppleScript files (shebang would work too), but we still need the idleberg extension for AS syntax coloring.

johnelm avatar Apr 19 '18 17:04 johnelm

@johnelm

You can get both of these, plus the missing JS language support, by treating your JXA scripts as regular Javascript:

Thanks, John. I really appreciate your help. I have not been able to install your node-jxa yet (I need instructions using npm to install), but using: #!/usr/bin/env osascript -l JavaScript with Code Runner seems to work OK, except for access to the Application.currentApplication() object. I'll post another issue to address that.

One question here: Is it possible to associate the ".jxa" extension with JavaScript in VSC? I'd like to continue using it to denote my JXA scripts (vs my pure JavaScripts)?

Many thanks.

JMichaelTX avatar Apr 19 '18 19:04 JMichaelTX

hey @JMichaelTX..

node-jxa installation Ah I see.. node-jxa has to be installed globally, maybe that's why it's not working.

I should have included the npm installation command in addition to the yarn version.. (done just now). The command using npm is :

npm install -g node-jxa  #  ( `-g` can also be `--global`)

adding jxa file association for javascript Yep you can do this by adding this line to your settings.json (you probably want to use your user settings, not workspace settings):

"files.associations": { "*.jxa":"javascript" },

Correction.. see next comment ~It's a bit unpredictable though, since the idleberg extension also contributes a .jxa association. I just tested it and recognized it as Javascript the first time, but as 'Javascript for Automation (JXA)' the second time. I hope there's a way to force it to choose the former but I don't know how at the moment.~

johnelm avatar Apr 19 '18 19:04 johnelm

Quick update here @JMichaelTX .. It looks like my "file.associations" line in my setting.json was deleted by the settings sync extension that I'm using.. so I put it back permanently.

With that bit in my config, it does select Javascript for .jxa files repeatably, even though I still have the idleberg extension installed. I'm editing my last post accordingly.

johnelm avatar Apr 19 '18 20:04 johnelm