Skip to main content
Known Participant
Feb 21, 2018 3:33 PM UTC
Answered

Create a txt file in Extendscript

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...

This topic is closed to new replies. Start a new post to keep the conversation going.
Tomas Sinkunas
Tomas SinkunasCorrect answer
Legend
Feb 21, 2018 4:13 PM UTC

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;

    }

}

11 replies

Tomas Sinkunas
Tomas SinkunasCorrect answer
Legend
Feb 21, 2018 4:13 PM UTC

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;

    }

}

Translate
Known Participant
Feb 21, 2018 5:00 PM UTC

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

}

Translate
Tomas Sinkunas
Legend
Feb 21, 2018 6:07 PM UTC

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

    // file exists

}

Translate
Mathias Moehl
Community Expert
Community Expert
Feb 21, 2018 3:42 PM UTC

JFile.write(content); 

instead of

File.write(content); 

?

Translate
Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Known Participant
Feb 21, 2018 3:45 PM UTC

haha...

didn't see that.

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

Translate