JavaScript for Automation - how to get the path of the current script?

Viewed 477

In macOS' JavaScript for Automation (JXA), how to get the path to its own script?

That is, the equivalent of the following AppleScript snippet

path to me

AppleScript path result

I've tried the following JavaScript snippet, but the resulting path points to the Script Editor app, despite the script having been saved as a script bundle (*.scptd).

app = Application.currentApplication()
app.includeStandardAdditions = true
pathToMe = app.pathTo(this)

JavaScript path result

The underlying reason behind this is for a JXA script bundle to contain additional resources that can be referenced and used by the script itself.

Note for those downvoting or voting to close, I've been through these and none of it worked effectively:

As shown in the screenshots above, app.pathTo(this) points to the Script Editor. The question is about getting the script bundle, just like what the AppleScript language option is able to do.

OS version: macOS 11.4 (20F71)
Processor: Apple Silicon (M1)
Script Editor: Version 2.11 (225)

0 Answers
Related