Forums > Javascript

Problem accessing relative files in File class

Sep 23 2009 | 9:14 pm

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 ("hello.") and test/ ("there!").

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

// Two ways to get to the same file.

// But I can't see a file in a subdirectory!


Sep 26 2009 | 3:15 pm

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

Sep 30 2009 | 9:44 pm

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) + ')');
Oct 23 2009 | 4:58 pm

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.

Jan 15 2010 | 11:22 pm

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.

Jan 15 2010 | 11:30 pm


Very clear then. Thanks!

Perhaps update to indicate that?

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

Forums > Javascript