Can't set where my JSON file gets written to.

daveronan's icon

Hi Max community,

I want to specify the directory where my json gets saved to, but it keeps saving the file in the application directory. I want it to save it in a folder below where the patch is saved. Below is the code I'm currently using. Can anyone help

f = new File("DrumClipData.json", "write", "TEXT");

    if (f.isopen) {
        post("writing string to file: " + f.filename + "in: " + f.foldername + "\n");
        f.writestring(JSON.stringify({data: retClips})); //writes a string
        f.close();
    } 
    else {
        post("could not create file: " + f.filename + "in: " + f.foldername + "\n");
    }
daveronan's icon

Also, I'd like to add I've tried putting a path before the filename and that doesn't seem to work.

daveronan's icon

Figured it out. Needed to pass the path correctly using the outlet of the thispatcher object.

Jay Walker's icon

Just in case someone else is looking for something a bit more basic, you can just reference the full path as a variable.


var memstr;
var mem = new Object();
var UI = new Object();
var p = "/Users/username/Folder Number 1/writeToJson.json";
function write(){
    read();
    anything(trackNumber, currentClip, arrayOfParamValues.join(' '));
    var jase = JSON.stringify(UI,null,'\t');
    var path = p;
    var fout = new File(path,"write","TEXT");
    if (fout.isopen) {
            fout.writeline(jase);
            fout.close();
            post("\nJSON Write",path);
        } else {
            post("\ncould not create json file: " + path);
        }
}