Copy link to clipboard
Copied
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!
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
oh, that explains it hahaha
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
It works! Thanks for your help, Charu Rajput! And everyone!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more