21

I want to get the absolute folder path of current running Extendscript.

i.e __DIR__ in php (or) __dirname in nodejs. Equivalent in Extendscript.

Kara
  • 6,115
  • 16
  • 50
  • 57
Alagu
  • 2,864
  • 3
  • 26
  • 40

5 Answers5

44

Found it myself, it is $.fileName

For the folder name, it is (new File($.fileName)).parent

Alagu
  • 2,864
  • 3
  • 26
  • 40
  • 1
    FYI (if anybody is following), the folder path is returned *without* any trailing backslach, and it is already in extendscript format, for example: `/c/some%20folde%20with%spaces/some/folder` -but the script likes this. – bgmCoder May 08 '13 at 02:02
  • 1
    Might I clarify: `$.fileName` returns the absolute path to the **current executing script**, not the opened file (e.g. the .ai file you're editing in Illustrator). – caiosm1005 Apr 24 '14 at 22:14
  • At least in Illustrator, this only works when you open the script in ExtendScript, and execute it. It doesn't work when you open it directly. – Jorjon Apr 28 '14 at 03:02
  • 1
    Mind you, while `$.fileName` works fine in the context of "pure" scripting execution, it may return garbage (like a random integer) if you evaluate in the context of an HTML Panel in some Adobe applications such as Photoshop. In that case, if you need to get the absolute path of a JSX, you may want to use `CSInterface.prototype.getSystemPath()` – Davide Barranca Oct 10 '17 at 22:19
  • 2
    var scriptFolder = (new File($.fileName)).parent.fsName; – andrewchan2022 Oct 20 '20 at 09:38
5

I wrote an article about this you may find helpful, :)

essentially:

var myPath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/');
var myScriptPath = (File(app.activeScript.fullName).parent.fsName).toString().replace(/\\/g, '/');

alert('Document path is: ' + myPath + ' , and script path is: ' + myScriptPath);

mim,

mim
  • 1,301
  • 14
  • 24
3
File($.fileName).path;

Seems to do the job nowadays

Maryan Hutsul
  • 53
  • 1
  • 8
1

If you need to reference files relative to the active document you can use app.activeDocument.filePath.

Nicolas Zanotti
  • 2,949
  • 1
  • 14
  • 7
-1

app.activeScript.parent should do the trick - note that if you use a windows file link, you get the actual script location, not the one of the .lnk-file.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221