Forums > Javascript

String declaration and opening Files

Apr 11 2008 | 7:17 pm

I ran into an interesting max javascript feature yesterday. I found the workaround, but thought I’d mention it. Basically, initializing a String at declaration will interfere with testing for a file with the same name as that string.

Or, more verbosely, the following function will always report that the file with the filename passed to it exists, even if the file does not.

function checkForFile(fileName) //check to see if file exists
var s = new String(fileName);
f = new File(s, "read");
if (f.isopen) //if succeed in opening file
post("found file: " + fileName + "n");
return (true);
else //file doesn’t exist
f.close(); //anal retentively close file
post("could not find file: " + fileName + "n");
return (false);

If I change the string declaration to be like this, however:

var s = new String();
s = filename;

Then it works fine. Don’t pick on programming style, by the way. I know that it is fully unnecessary to declare s in the first place. This was just a concise example. It is not specific to a local function either. If a string is declared with that text outside of the function, the function still misreports the file.

String is obviously part of javascript 1.5, but File IO is a max/msp add. I’m curious about what’s going on under the hood to make that happen.


Apr 22 2008 | 12:25 am

can’t help with an answer,
but can confirm that is happening with me too.

I had struck this the first time i used f.isopen
and had incorrectly thought that it could not be used to check for the existence of files and have been laboriously trawling through folder objects to check for files.

so sorry that i can’t help but thank you for posting this

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

Forums > Javascript