Forums > Javascript

File.filename bug?

March 1, 2014 | 7:54 pm

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(patcher.name+".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();
}


March 1, 2014 | 8:02 pm

Note- I accidentally attached the file twice. They are the same. Not sure how to delete it.


March 5, 2014 | 5:25 pm

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(patcher.name+".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 window
post("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();
}


March 5, 2014 | 5:40 pm

ah – nice, yep, that’s it all right

-A


Viewing 4 posts - 1 through 4 (of 4 total)