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

Create a txt file in Extendscript

Community Beginner ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Hey!

im trying to create a script that creates a txt file but i can't do that...

that is the function code:

var scriptFolderPath = File($.fileName).path; // the URI of the folder that contains the script file 

var TimerFolderPath = scriptFolderPath + encodeURI("/ProjectTimers_sources"); // the URI of the folder for your script resources 

StartButton.onClick = function(){

var JFile = new File(TimerFolderPath + encodeURI("/File.txt"));

var content="That is a text file";

File.write(content);

alert("File created!");

}

and it doesn't creates any file...

TOPICS
Scripting

Views

13.4K

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

Advocate , Feb 21, 2018 Feb 21, 2018

Give this one a shot.

First you better check if you have permissions to Write Files and Access Network.

Next, you probably need to open fine before writing to it.

var scriptFolderPath = File($.fileName).path; // the URI of the folder that contains the script file   

var TimerFolderPath = scriptFolderPath + encodeURI("/ProjectTimers_sources"); // the URI of the folder for your script resources   

StartButton.onClick = function() {

    var JFile = new File(TimerFolderPath + encodeURI("/File.txt"));

   

...

Votes

Translate

Translate
Community Expert ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

JFile.write(content); 

instead of

File.write(content); 

?

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects

Votes

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 Beginner ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

haha...

didn't see that.

but, the file is not being created yet...

Votes

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
Advocate ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Give this one a shot.

First you better check if you have permissions to Write Files and Access Network.

Next, you probably need to open fine before writing to it.

var scriptFolderPath = File($.fileName).path; // the URI of the folder that contains the script file   

var TimerFolderPath = scriptFolderPath + encodeURI("/ProjectTimers_sources"); // the URI of the folder for your script resources   

StartButton.onClick = function() {

    var JFile = new File(TimerFolderPath + encodeURI("/File.txt"));

    var content = "That is a text file";

   

    if (!canWriteFiles()) return null;

    writeFile(JFile, content);

   

    // File.write(content);

    alert("File created!");

};

function writeFile(fileObj, fileContent, encoding) {

    encoding = encoding || "utf-8";

    fileObj = (fileObj instanceof File) ? fileObj : new File(fileObj);

    var parentFolder = fileObj.parent;

    if (!parentFolder.exists && !parentFolder.create())

        throw new Error("Cannot create file in path " + fileObj.fsName);

    fileObj.encoding = encoding;

    fileObj.open("w");

    fileObj.write(fileContent);

    fileObj.close();

    return fileObj;

}

function canWriteFiles() {

    if (isSecurityPrefSet()) return true;

    alert(script.name + " requires access to write files.\n" +

        "Go to the \"General\" panel of the application preferences and make sure " +

        "\"Allow Scripts to Write Files and Access Network\" is checked.");

    app.executeCommand(2359);

    return isSecurityPrefSet();

    function isSecurityPrefSet() {

        return app.preferences.getPrefAsLong(

            "Main Pref Section",

            "Pref_SCRIPTING_FILE_NETWORK_SECURITY"

        ) === 1;

    }

}

Votes

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 Beginner ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

It is working!

thanks

i have another question.

i want to create an if statement that checks if the file already exists. how can i do that?

for example:

if(***the text file we have created exists***) {

bla bla

}

Votes

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
Advocate ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

if (File("pathToFile.extension").exists) {

    // file exists

}

Votes

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 Beginner ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Not working...

var p=app.project;

var projTLayersFound=[];

var projTextName = "this project";

//get project name:

var projName = p.file.name.replace(/%20/g, "");

var didNotFindOne = true;

///Other

if (File(TimerFolderPath + encodeURI("/" + projName + ".txt")).exist) {

alert("if");

  } else {

   StartButton.onClick = function() {

    var TimerFile = new File(TimerFolderPath + encodeURI("/" + projName + ".txt"));

    var content = "That is a text file";

    

    if (!canWriteFiles()) return null;

    writeFile(TimerFile, content);

    

    // File.write(content);

  alert(projName);

    alert("File created!");

};

}

Votes

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
Advocate ,
Feb 24, 2018 Feb 24, 2018

Copy link to clipboard

Copied

Apologies, I had a typo in code: it should have been File().exists and not File().exist

Votes

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 Beginner ,
Feb 25, 2018 Feb 25, 2018

Copy link to clipboard

Copied

Worked! thanks

Votes

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
New Here ,
Aug 08, 2018 Aug 08, 2018

Copy link to clipboard

Copied

If I call the function

app.preferences.getPrefAsLong( 

            "Main Pref Section", 

            "Pref_SCRIPTING_FILE_NETWORK_SECURITY" 

        );

in Illustrator 2018 22.1, I get the error message "app.preferences.getPrefAsLong is not a function". Is this function deprecated?

Votes

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
LEGEND ,
Aug 15, 2018 Aug 15, 2018

Copy link to clipboard

Copied

theodorm34189694  wrote

If I call the function

in Illustrator 2018 22.1 ....

You're responding to a topic in AFTER EFFECTS scripting. There's your clue.

Votes

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
New Here ,
Aug 15, 2018 Aug 15, 2018

Copy link to clipboard

Copied

LATEST

Thanks for the hint... I really need to brush up my reading skills

Votes

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