Problem accessing relative files in File class

Sep 23, 2009 at 9:14pm

Problem accessing relative files in File class

I can get to files with an absolute path. I can get to files in the current directory.

But I can’t get to files in subdirectories using a relative path.

Here’s code. My local directory contains a file test.data (“hello.”) and test/test.data (“there!”).

function evalfile(name) {
    return post('(' + new File(name).readstring(100) + ')n');
};

// Two ways to get to the same file.
evalfile('/Users/tom/Documents/TomSwirly/Max/test.data');
evalfile('test.data');

// But I can't see a file in a subdirectory!
evalfile('/Users/tom/Documents/TomSwirly/Max/test/test.data');
evalfile('test/test.data');
evalfile('./test/test.data');

Results:

(hello.)
(hello.)
(there!)
(null)
(null)
#45584
Sep 26, 2009 at 3:15pm

No replies here? Does someone want to at least try to reproduce this?

This isn’t a game-stopper for me but it’s making life somewhat inconvenient. I have a performance command system that’s driven by lots of little data files and because of this they litter my main directory instead of residing in a tiny subdirectory.

I’d gladly fix this little bug if you gave me access to the source code! Very Happy

#164392
Sep 30, 2009 at 9:44pm

All right, I worked around this bug. (The documentation states that relative paths are possible and they aren’t, so it’s a bug.)

I simply get the foldername for the js file itself, and use that to construct relative paths. My unit tests all pass!

(This probably only works on Mac, not Windows, I didn’t test it there.)

var _root_directory = new File('read_json.js').foldername;
var _separator = '/';

function read_json(name) {
  if (name.indexOf(':' ) == -1 && name[0] != _separator)
    name = _root_directory + _separator + name;
  return eval('(' + new File(name).readstring(1000000) + ')');
};
#164393
Oct 23, 2009 at 4:58pm

One more update on this little issue!

This MAXBUG workaround has been working perfectly well for me – but I realized it wasn’t quite doing what I thought!

var _root_directory = new File('read_json.js').foldername;
var _separator = '/';

…does NOT necessarily identify the directory containing the JS code – instead it identifies the directory where the containing Max patch lives.

This actually works out very well for me – it means I have to have a separate data directory for my unit tests, which is just what I want. But it’s important to be clear about what’s going on.

#164394
Jan 15, 2010 at 11:22pm

Relative paths in Max are relative to the Max Application. There is no current syntax for patcher or javascript file relative paths.

Your workarounds are the way to go.

#164395
Jan 15, 2010 at 11:30pm

Aha!

Very clear then. Thanks!

Perhaps update http://www.cycling74.com/docs/max5/vignettes/js/jsfileobject.html to indicate that?

#164396

You must be logged in to reply to this topic.