Tag Archives: Relative path

manage paths in Adobe Extend-Script

Adobe Exten-Script (source: internet)
Adobe Exten-Script (source: internet)

Managing paths obtained from Adobe Extend-Script, Specially when you want to share these paths with an external 3th application (to do things which aren’t possible with just ExtendScript) is kind of difficult!
For instance let’s say we want to get the Path of an In-Design file being opened (and in front) in in-Design, we may simply do:


And get:

alert(app.activeDocument.filePath);
alert(app.activeDocument.filePath);

But there is obviously 3 (+1 not obvious here!) possible problems with this path (even if you can transfer that variable as easily as shout it out in an alert! look at #3):

  1. the Tilde (~) character at the beginning, an environment variable meaning “Home Directory“, which is not a recognizable standard character in all the different programing environments (for instance it’d be %~dp0 in Batch scripting and user.home, (more or less!) in Java) although it is supposed to be the absolute path using above code!
  2. existence of Slash characters (/) in the path instead of Backslash (\), making it hard (not so much tho!) to send it through a JSON package for example (or save it in a batch file -again- for instance), be-cause in most environments it’d be considered as a escape character and not a path level separator.
  3. It is an object and not a string (apparently!) and you can send, save or sometimes even use the myPath variable for instance directly! (it will give you an [object] instead of the path, which I guess should be the parent File object itself)
  4. in addition if our file wasn’t in our Desktop folder and was in a disk drive instead, we would have this problem too:
    alert(app.activeDocument.filePath);
    alert(app.activeDocument.filePath);

    as you can see, an additional Slash character (/) before the Drive letter (c)! which is incomprehensible to me why it is!! but it seems that Extend-Script itself doesn’t have any problem with it, but as I said when you want o communicate with 3th party apps it will become a problem immediately!

  5.     So as you may guess there is a lot of solutions for any and each of mentioned problems, but here I just wanted to suggest the most convenient one I figured out after trying a lot of different shenanigan techniques!
    Here we used Regular Expression Patterns to Replace() all (by using the g pseudo) the Slashes with Backslashes and by using fsName instead of filePath eliminating other problems (like getting “relative paths” (or fake absolutes!) instead of absolutes and the beginning slash character)
    And the result:

    alert(app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/');
    alert(app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, ‘/’);

    Awesome! 😀
    Please let me know if you have a better idea to this or if you see a problem or something, I appreciate it 😉
    mim,