js object: Loading javascript files in subdirectory

williamcotton's icon

Is it possible to load javascript files from a subdirectory? My current project's root path is getting pretty messy, and I want to organize my javascript files.

Something akin to: [js ./javascripts/script1.js]

Luke Hall's icon

You can either add files to the search path using Options > File Preferences or use the [filepath] object to do this temporarily. If you want to include multiple javascript files in the current file then save the code below somewhere in the jsextensions folder.

lh

/*
* function to add other javascript files to your project
*
* first argument must be "this" to set scope
* second argument specifies the file to include
* relative paths must start wih a "/" separator
* this function uses eval() so be careful
*
* example
*
* include(this,"/local.js"); // same directory
* include(this,"/lib/js/functions/below.js"); // navigate lower
* include(this,"/../../above.js"); // navigate higher
* include(this,"/../../sub1/sub2/both.js"); // combine both
* include(this,"Macintosh HD:/Users/me/Desktop/other.js"); // absolute path
*/

function include(scope,dest) {
    var mem = "";
    var struct = /((/..)*)(.+$)/;
    var parent = /(.+)/[^.]+.w+$/;
    var levels = struct.exec(dest)[1].length/3;
    var name = struct.exec(dest)[3];
    var target;
    if (dest.charAt(0) != "/") {
        target = dest;
    } else if (!levels) {
        target = scope.patcher.filepath.match(parent)[1]+name;
    } else {
        target = scope.patcher.filepath;
        for (i=-1; i
tdaeik's icon

hi,
when trying to use this, i get an error:

js: includefiles.js: Javascript TypeError: s has no properties, line 87

i'm a bit confused as there's no line 87 in the file.
i have the above file saved in a .js file called includefiles.js in 'Max5/Cycling '74/jsextensions/'
basically i just have my .js file with a single line at the top:

include(this, "/../../../examples/javascript/math/dynexpr.js")

can anyone offer any assistance?
cheers,,

edit: sorry, it still works. so i guess it doesn't matter?

Luke Hall's icon

The way it reads the specified file into the current project is done line by line and the maximum line length is hard-coded to 800 characters so if you have code which exceeds this (if you've used a javascript compressor for the web which removes line breaks) then it is likely to break. It's also important to make sure you're looking in the right directory. Lastly the error might be in the file you are trying to include, so remember to check that too.

lh

tdaeik's icon

cheers. so i guess the error wasn't really with the include() function, but rather with the file it was including, namely the dynexpr.js (which itself has only 54 lines of code). there was a variable s declared like:

expression = s.toString().replace(/"/g,""); // strip quotes if present

which caused it to complain because i didn't include the default argument in my new js object (the script of which imported the old dynexpr.js). so thats all cool, cheers again for your help!

yair reshef's icon

*bump*

trying to run this i get an error

js: includes.js: Javascript TypeError: scope.eval is not a function, line 49

refering to scope.eval(mem);

win7/6.0.4

Jan M's icon

eval() is deprecated ans not supported buy Max6 anymore.

sxa's icon

>>eval() is deprecated ans not supported buy Max6 anymore.

Could you elaborate? Is this in the documentation? I use my own simple eval()-based include mechanism and its working fine in 6.04/win7.

hems's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Eval is workingo n Max6:

I would be surprised if they really remove that functionality without adding something to replace it.

orange_glass's icon

Hi it appears that the code in Luke's original post cuts off mid function.

Is anyone else seeing that?

patrickkidd's icon

I am also seeing the code cut off. I would love to have that snippet!

patrickkidd's icon
/*
* lh.jsextensions
*
* first argument must be "this" to set scope
* second argument specifies the file to include
* relative paths must start wih a "/" separator
* this function uses eval() so please be careful
*
* include(this,"/local.js"); // same directory
* include(this,"/lib/functions/below.js"); // navigate lower
* include(this,"/../../above.js"); // navigate higher
* include(this,"/../../sub1/sub2/both.js"); // combine both
* include(this,"Macintosh HD:/Users/directory/sub-directory/other.js"); // absolute path
*/
 
function include(scope,dest) {
    var mem = "";
    var struct = /((\/\.\.)*)(.+$)/;
    var parent = /(.+)\/[^.]+\.\w+$/;
    var levels = struct.exec(dest)[1].length/3;
    var name = struct.exec(dest)[3];
    var target;
    if (dest.charAt(0) != "/") {
        target = dest;
    } else if (!levels) {
        target = scope.patcher.filepath.match(parent)[1]+name;
    } else {
        target = scope.patcher.filepath;
        for (i=-1; i
patrickkidd's icon

Nuts, although the call to match() for relative paths returns null for some reason so it's not working for me. ugh..