johnelm / node-jxa

Use your favorite node.js modules (and JS editor) for your Javascript OSX automation scripts
74 stars 4 forks source link

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

Open JMichaelTX opened 6 years ago

JMichaelTX commented 6 years ago

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:
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.

johnelm commented 6 years ago

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 ):

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

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.

JMichaelTX commented 6 years ago

@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.

johnelm commented 6 years ago

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 commented 6 years ago

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.