Copy link to clipboard
Copied
As file API is documented poorly I wonder if anybody does know if it is even possible to open a text file (.txt) externally by using JavaScript with in Illustrator – and if so how this might work 😉
Scenario here:
In Illustrator I'd like to open up a .txt file by pressing a button so the user can easily edit it in an non specific external application (system based default).
Any help is very much appriciated.
And as always: No it can not be done manually for a reason!
thanks in advance
Nils
1 Correct answer
var file1 = new File('~/Desktop/text1.txt');
if (file1.exists) {
file1.execute();
} else {
alert("No such file.");
}
Explore related tutorials & articles
Copy link to clipboard
Copied
var file1 = new File('~/Desktop/text1.txt');
if (file1.exists) {
file1.execute();
} else {
alert("No such file.");
}
Copy link to clipboard
Copied
Awesome! Thanks a lot for the quick reply @femkeblanco!
Here's my final script which checks for a specific file within the user folder no matter if on mac or win (just left 2 alerts in the script for those who want to tryout):
#target illustrator
if (app.documents.length > 0) {
var myUserFolder = Folder.userData.fsName;
alert(myUserFolder);
//Mac: Users/nmb/Library/Application Support
//Win: C:\Users\nmb\AppData\Roaming
var docRef = app.activeDocument;
var artboardRef = docRef.artboards;
var scriptRef = new File($.fileName).path;
var separator = '\\'; //windows only
getSeparator();
var myFileName = 'file01.txt';
var myFile = new File(myUserFolder + separator + myFileName);
alert(myFile);
if (myFile.exists) {
myFile.execute();
} else {
alert("File not found!");
}
// get separator based on OS
function getSeparator() {
if ($.os.toLowerCase().indexOf('mac') != -1) {
separator = '/'
lineBreak = '\n';
} else {
separator = '\\';
lineBreak = '\r';
}
}
}

