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