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
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)
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:
- How to get POSIX path of the current script's folder in JavaScript for Automation?
- How do I get the Scripts folder in the application bundle?
- Applescript indicates resource not found when trying to call within inside the app
- https://macscripter.net/viewtopic.php?id=43445
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)