Run Current VSC JXA Script in Script Editor Macro (v9.2)

Use Case

  • Make it Easy to Debug JXA Scripts written in Visual Studio Code (VSC)
  • In order to use the Safari JXA Debugger, we must first run the script from Script Editor.
  • Simplify this entire process of JXA debugging.

==UPDATED==: 2021-06-05 18:22 GMT-5

  • ADD Video showing how to debug without using Script Editor

Video -- Demo Using Safari JXA Debugger Directly From Visual Studio Code [VSC, vscode]

While the below macro works fine, I have now learned that we really don't need to use Script Editor to debug JXA with Safari.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Video Demo

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Run Current VSC JXA Script in Script Editor

-~~~ VER: 2.0    2021-06-04 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Run Current VSC JXA Script in Script Editor.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Run Current VSC JXA Script in Script Editor
    • Debug Using Safari JXA Debugger

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. See the below "How to Use" Comment Action
  3. This macro is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this Macro .
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    • For more info, see KM Wiki article on Macro Activation
      .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • Type ⌃⌘P -- Telll VSC to Set Clipboard to Path of Active File
      • Change VSC, or change Action to use your Shortcut

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.12.6 (Sierra)+
  3. Visual Studio Code (VSC)
  4. Script Editor
  5. Safari

TAGS: @VSC @ScriptEditor @Safari @JXA @Debug

2 Likes

Remind me why you want to involve Script Editor in this process ?

Because you can ONLY invoke the Safari JXA debugger from Script Editor (and maybe from Automator).

Just uploaded a revised video.

Tell my why you think that ?

The Safari JS debugger launches when you run code in VSC (with the Code Runner extension for example), as long you you have Safari set up properly.

(FWIW I never use Script Editor. Apart from the limited memory allocation, its stringification post-processor gives a confused version of the value that is actually returned to a KM Execute JXA action, whereas you can see that value directly and exactly with the output from Code Runner)

Apple has often stated that, and I have it also stated my many experienced JXA scripting developers.

I've never seen it, nor have I seen any detailed instructions on how to setup VSC to do this. Perhaps you could provide:

  1. A short video demo showing this process
  2. Detailed instructions on how to setup VSC and Safari to enable this process.

EDIT 2021-06-04 17:48 GMT-5

There are currently 392 OPEN issues, and over 300 closed issues on the Code Runner GitHub site. It does not sound like a very reliable VSC extension to me.

Perhaps some misunderstanding has arisen there.

(I remember thinking the same, for a while, in relation to Atom some years ago, and being corrected by others on Stack Overflow).

With this Safari setting:

The context that launches the JS Context can just as well be VSC or TaskPaper as anything else, provided that the inter-app automation permissions are given.

I wonder if the latter have contributed some confusion ?

With Code Runner installed in the normal way, a Run Code command appears in the VSC command palette. The default keystroke is ⌘⌥N I think, (see the extension settings), but I personally have it assigned to something else.

(Code Runner has always worked very well for me – with 7.5M downloads and 4.5 stars, it might be disappointing not to see a bit of repository chatter. Script Editor, in contrast, flakes out a bit more easily – a much more limited memory allocation for JS, and not much evidence of maintenance in recent years. )

debugFromVSC

PS, it might be worth simplifying your first tests by launching scripts directly at the terminal prompt.

This, at the Terminal.app prompt, for example, also launches the Safari Web Inspector, as long as Terminal has the automations permissions, Safari has the Developer settings, and the script contains a debugger; line in reachable code:

/usr/bin/osascript -l JavaScript ~/Desktop/sample.scpt

The application process from which the JSContext is invoked is relevant only to the extent that it needs sandbox permission for automation.

Just updated my OP above to add the new video.

While the macro I originally posted works fine, I have now learned that we really don't need to use Script Editor to debug JXA with Safari.

Thanks for pointing this out, @ComplexPoint .

I have independently verified this, and posted a new video showing it.
I didn't discover your new posts until I updated mine.

So, we are in agreement: we really don't need to use Script Editor to debug JXA with Safari.

My understanding now is that originally, when JXA was first released, Script Editor was required. But later (I don't know when) Apple make some updates to allow it to work with osascript. So now many third party tools can directly invoke the Safari JXA Debugger❗

So the world now has my YouTube to show this. I hope others will find it useful.
I have done extensive searching, and previously I did NOT find any Videos or articles on the 'net that indicated how to debug JXA from VSC directly using the Safari Debugger.

1 Like

Thanks @JMichaelTX so much for providing this: I'm only a beginner when it comes to JXA so this is really very useful to me.

1 Like

Jim - Sorry if this question has already been answered in this thread.

Does this work for a JXA script that has nothing to do with the browser? I'm confused as to how Safari can debug it, if it isn't browser-based.

Thanks.

Absolutely! The Safari JXA Debugger supports OSA automation objects.
In fact, I show that in my video demo.

1 Like

Any JSContext can be registered (by the developer) for use with the Safari Debugger.

See, for example:

[https://webkit.org/web-inspector/enabling-web-inspector/]( Enabling Web Inspector | WebKit )

and more generally:

[https://developer.apple.com/documentation/javascriptcore/jscontext?language=objc]( https://developer.apple.com/documentation/javascriptcore/jscontext?language=objc)

1 Like