Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using relative file paths to open a document from Illustrator .ait with JavaScript

Community Beginner ,
Oct 01, 2021 Oct 01, 2021

Hello, everyone,

I am writing a script in JavaScript that opens a file from a particular Illustrator template file depending on what the user enters. I have been able to do it using absolute file paths, but my question is can I use relative paths from where the script is to where the .ait files are? I plan on having them all in the same folder, but so far nothing I have tried using relative paths has worked. Below is an example of the absolute path:

app.open(new File (//"/C/Users/blahblah/Otherfiles/Proof_Template_Standard.ait")

I want to give the script and the template files to my co-workers who are on a different network, and setting up the absolute file paths for each copy of the script and template files seems a bit much. I thought that if I can use relative paths and because the script lives in the same folder as the templates that it might simplify things.

Does anyone have any suggestions, or can it not be done with relative paths?

 

Thanks!

TOPICS
Scripting
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 01, 2021 Oct 01, 2021

If I understand your question correctly, is your script and template file will reside in tthe same folder?

If yes, then you can just use

 

var _scriptPath = $.fileName;
var _separater = '';

//Code to get separater based on OS
if ($.os.toLowerCase().indexOf('mac') != -1) {
    _separater = '/'
} else if ($.os.toLowerCase().indexOf('window') != -1) {
    _separater = '\\';
}
var _path = _scriptPath.substring(0, _scriptPath.lastIndexOf(_separater))
app.open(File(_path + '/Test.ait'))

 

To Test, sa

...
Translate
Adobe
Community Expert ,
Oct 01, 2021 Oct 01, 2021

If I understand your question correctly, is your script and template file will reside in tthe same folder?

If yes, then you can just use

 

var _scriptPath = $.fileName;
var _separater = '';

//Code to get separater based on OS
if ($.os.toLowerCase().indexOf('mac') != -1) {
    _separater = '/'
} else if ($.os.toLowerCase().indexOf('window') != -1) {
    _separater = '\\';
}
var _path = _scriptPath.substring(0, _scriptPath.lastIndexOf(_separater))
app.open(File(_path + '/Test.ait'))

 

To Test, save this script and Test.ait file in the same folder. Please cross check separater for Windows. Not tested on WIndows. I hope this will help you. 

Best regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2021 Oct 01, 2021

Hi all, I'd like to post this because it takes a slightly different approach and, like @Charu Rajput, I'd like to know from someone whether it works on Windows—I've only tested just now on MacOS. I like it because it takes advantage of the build in properties of Extendscript File and Folder objects.

 

This example makes a new folder in the same location as the document, which isn't what you want, but I hope for learning you can apply to your situation. I've included how to get the folder object that the script resides in, so just replace folderContainingDocument with folderContainingScript.

 

 

 

var folderContainingScript = new File($.fileName).parent;
$.writeln('folderContainingScript = ' + folderContainingScript);
$.writeln(folderContainingScript.constructor.name);

if (
    app.documents.length > 0
    && app.activeDocument.saved == true // there's no folder if not saved
) {
    // filePath property returns a Folder object
    var folderContainingDocument = app.activeDocument.path;
    $.writeln('folderContainingDocument = ' + folderContainingDocument);
    $.writeln(folderContainingDocument.constructor.name);

    // example: make a folder relative to the document folder
    var myNewFolder = new Folder(folderContainingDocument + '/New Stuff/');
    if (!myNewFolder.exists) myNewFolder.create();
    // reveal that folder
    myNewFolder.execute();
}

 

 

 

 

Note that when you treat a File or Folder object like a string, for example this bit: folderContainingDocument + '/New Stuff/' it converts the File/Folder into a path. If the above code doesn't work on windows, try making this explicit by doing folderContainingDocument.fsName.

- Mark

 

Edit: fixed wrong document property—thanks to Carlos.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2021 Oct 01, 2021

Hi Mark, it does work on Windows. But I had to change the line that gets the document path.

 

filePath is not a property, I'm surprised it works on your end.

 

//var folderContainingDocument = app.activeDocument.filePath;

var folderContainingDocument = app.activeDocument.path;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2021 Oct 01, 2021

Hi Carlos, thanks for testing. Um, I now realise that the property filePath is for *Indesign*. I even tested the code *in Indesign* without realising. I am clearly sleep-deprived and should stop posting! I'll see myself out.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2021 Oct 01, 2021

oh, that explains it hahaha

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2021 Oct 02, 2021

@m1b 

Thanks Marks, for sharing different approach to achieve this.

 

Best regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 04, 2021 Oct 04, 2021

Thanks for the help, everyone! 

Charu Rajput, I do intend on having the script file and the Illustrator template files in the same folder. I have a lot of things to try thanks to you folks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 04, 2021 Oct 04, 2021
LATEST

It works! Thanks for your help, Charu Rajput! And everyone!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines