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

Open txt file externally using javascript in Illustrator?

Community Expert ,
Oct 09, 2022 Oct 09, 2022

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

TOPICS
Scripting

Views

69

Likes

Translate

Translate

Report

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

Advisor , Oct 09, 2022 Oct 09, 2022

 

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

 

Likes

Translate

Translate
Advisor ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

 

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

 

Likes

Translate

Translate

Report

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 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

LATEST

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

}

 

Likes

Translate

Translate

Report

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