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]

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

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

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?

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

lh

#169638
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!

#169639
Mar 16, 2012 at 7:37pm

[EDIT]

http://www.cycling74.com/forums/topic.php?id=38324

*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

#169640
Mar 16, 2012 at 10:03pm

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

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

#169642
Jan 30, 2014 at 5:26am

Eval is workingo n Max6:

<code>

– Pasted Max Patch, click to expand. –

</code>

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

#279553
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?

#282457

You must be logged in to reply to this topic.