Problem accessing relative files in File class

Tom Swirly's icon

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)

Tom Swirly's icon

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!

Tom Swirly's icon

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) + ')');
};

Tom Swirly's icon

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.

Joshua Kit Clayton's icon

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.

Tom Swirly's icon

Aha!

Very clear then. Thanks!