File.filename bug?

    Mar 02 2014 | 3:54 am
    Hello All,
    The issue is this: The first time a new File object is assigned to a variable (say 'f') after script compilation, f.filename returns the file name by itself. Assigning a new file to the same variable again without recompiling will cause f.filename to return the entire absolute path to the file. Check out the attached patcher to replicate the problem. Enlightenment is very welcome.
    For convenience, here's the js code-
    var patcherDir = this.patcher.filepath.replace(".maxpat", "");
    function writeNpostname() { var awesomeFile = new File(patcherDir+"asauce.txt", "readwrite", ["TEXT"]); awesomeFile.writeline("I am the most awesome file"); post("filename: " + "asauce.txt"); post(); post("File.filename: " + awesomeFile.filename); post(); }

    • Mar 02 2014 | 4:02 am
      Note- I accidentally attached the file twice. They are the same. Not sure how to delete it.
    • Mar 06 2014 | 1:25 am
      K, figured it out. Pretty sure it's a reference problem. If you don't get rid of the old file reference (with File.close) before you run a new File constructor, it will cause some problems, one of them apparently being that it messes up the filename property. Another being that it will not open the file in memory so you can write and read from it.
      Check out the code below for a solution to the problem. I found it by posting all of the properties and looking for a difference between the first and the second time assigning the file. 'isopen' was 0 after the first assignment.
      Here's the working code:
      var patcherDir = this.patcher.filepath.replace(".maxpat", "");var awesomeFile;
      function writeNpostname() { if(awesomeFile) awesomeFile.close(); awesomeFile = new File(patcherDir+"asauce.txt", "readwrite", ["TEXT"]); awesomeFile.writeline("I am the most awesome file");
      //post all properties to max windowpost("access: " + awesomeFile.access); post(); post("byteorder: " + awesomeFile.byteorder); post(); post("eof: " + awesomeFile.eof); post(); post("filename: " + awesomeFile.filename); post(); post("filetype: " + awesomeFile.filetype); post(); post("foldername: " + awesomeFile.foldername); post(); post("isopen: " + awesomeFile.isopen); post(); post("linebreak: " + awesomeFile.linebreak); post(); post("position: " + awesomeFile.position); post(); post("typelist: " + awesomeFile.typelist); post(); post("———————————"); post(); }
    • Mar 06 2014 | 1:40 am
      ah - nice, yep, that's it all right