Skip to main content
Community Expert
October 9, 2022
Answered

Open txt file externally using javascript in Illustrator?

  • October 9, 2022
  • 2 replies
  • 661 views

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

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer femkeblanco

 

var file1 = new File('~/Desktop/text1.txt');
if (file1.exists) {
    file1.execute();
} else {
    alert("No such file.");
}

 

2 replies

femkeblanco
femkeblancoCorrect answer
Legend
October 9, 2022

 

var file1 = new File('~/Desktop/text1.txt');
if (file1.exists) {
    file1.execute();
} else {
    alert("No such file.");
}

 

Community Expert
October 10, 2022

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';
        }
    }

}