js object: Loading javascript files in subdirectory

Dec 14, 2009 at 10:34pm

js object: Loading javascript files in subdirectory

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]

Dec 14, 2009 at 10:58pm

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.


* 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
Feb 14, 2010 at 9:54am

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?

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

Feb 14, 2010 at 12:08pm

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.


Feb 14, 2010 at 2:20pm

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!

Mar 16, 2012 at 7:37pm




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);


Mar 16, 2012 at 10:03pm

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

Apr 20, 2012 at 10:11am

>>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.

Jan 30, 2014 at 5:26am

Eval is workingo n Max6:


– Pasted Max Patch, click to expand. –


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

Feb 28, 2014 at 8:48am

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

Is anyone else seeing that?


You must be logged in to reply to this topic.