'Indesign extended Script unable to create a file in Mac OS 10.16

I have a jsx script which i use for indesign scripting. The function below is used to write to a file. It works perfectly in windows. In mac it is not able to create not read the file from applications folder. Does it have to do with permissions in mac??

i did tried with applescript to create a file. The code worked fine without errors but the file is not been created. same with the below function. The code works perfectly. but I am not able to create the txt file inside the folder.

fileobject = "applications/foldername/filename.txt"
filecontent = "text"

function writeToFile(fileObj, fileContent){ 
    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;
}


Solution 1:[1]

I've tried this on MacOS 10.13 and it works fine:

function writeToFile(fileObj, fileContent) {
  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;
}

writeToFile('/Applications/folder/filename.txt', 'text');

enter image description here

Basically it should work on 10.16 as well. If it doesn't it could have to do with security settings. To save user files into system folders /Applications etc is not a good idea.

Could you try to save a file into user folders: ~/Desktop or ~/Downloads?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RobC