2

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)

adib
  • 8,285
  • 6
  • 52
  • 91
  • Does this answer your question? [How to get POSIX path of the current script's folder in JavaScript for Automation?](https://stackoverflow.com/questions/28773207/how-to-get-posix-path-of-the-current-scripts-folder-in-javascript-for-automatio) – Olian04 Jun 19 '21 at 13:32
  • @Olian04 No, I've found that answer before asking this one. As per above, `pathTo(this)` points to the Script Editor. – adib Jun 19 '21 at 14:02
  • 1
    What's interesting, is in **macOS High Sierra**, **macOS Mojave** and **macOS Catalina** your **Test 2** _code_ returns the _path_ of the _document_, but not so in **macOS Big Sur**. Maybe it's a bug. – user3439894 Jun 19 '21 at 14:54
  • @user3439894 I don't have a version of older macOS right now, so I can't test your statement. In any case, I added environment info for reference. – adib Jun 19 '21 at 15:13
  • RE: "so I can't test your statement." -- As I've already tested it, there is no need for you to do so. The whole point of me testing under the versions of **macOS** that I did is to show that under **macOS Big Sur** the same _code_ fails, which presents the possibility of a bug in **macOS Big Sur** in regard to how **Script Editor** process **JAX** _code_ . BTW I tested on an **Intel** based **Mac**. – user3439894 Jun 19 '21 at 15:26
  • @user3439894 This behavior remains the same in Monterey Beta 1 as with Big Sur 11.4, so it looks like it's here to stay. – adib Jun 19 '21 at 15:27
  • This may not be an answer to your question, because you specify needing a script bundle. But your code does work if saved as an Application and then the application is run not in Script Editor. Assign `app.pathTo(this)` to `myPath` and use `app.displayDialog(myPath.toString())` to display a dialog box. – Jerry Stratton Jun 19 '21 at 15:52
  • 1
    Features that work in AppleScript are often broken or missing in JXA. The Mac Automation team that wrote JXA abandoned it after release, and the team itself was disbanded by Apple in 2016 and its Product Manager sacked. Draw your own conclusions from that. If you want to automate desktop apps, best stick to AppleScript; awful language, but at least it works. If you want to write scripts in JavaScript, get Node.js. If you want to package those JavaScripts as apps (and don’t mind a 100MB executable), Electron is fast and easy to work with. And for general app development, there’s Swift. – foo Jun 20 '21 at 08:36
  • @foo Product Manager – this guy? https://macosxautomation.com/about.html – adib Jun 21 '21 at 01:26
  • Yes. Point being, you’re on your own with JXA as far as Apple is concerned: they haven’t touched it since 10.11 and the team that wrote it no longer exists, so stuff that’s broken stays broken. (AppleScript also went into maintenance mode in 2016, but at least AS was working and documented, with a modest but experienced community for user-to-user support.) – foo Jun 21 '21 at 06:28
  • 1
    FWIW I did try some experiments but couldn’t find a way to make `pathTo(…)` work for a `.scpt` file. That particular behavior relies on two arcane OSA features, which JXA probably doesn’t implement correctly/at all. It you save the script as an applet, `pathTo(this)`/`pathTo(null)` will return the path to the `.app` bundle (that behavior’s actually a quirk/feature of the `path to` scripting addition, so doesn’t rely on the same OSA hooks), as would using `NSBundle.mainBundle()…`. But as I say, it’s not worth spending a lot of time trying to solve as it’s abandonware anyway. – foo Jun 21 '21 at 06:59

1 Answers1

-1

I find a way to get current parent path, its worked for me:

var app = Application.currentApplication();
app.includeStandardAdditions = true
var path = app.doShellScript('pwd')
Nauxscript
  • 95
  • 1
  • 7