Forums > Javascript

js object: Loading javascript files in subdirectory


Dec 14 2009 | 10:34 pm

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 | 10:58 pm

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

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?

Feb 14 2010 | 12:08 pm

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

Feb 14 2010 | 2:20 pm

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 | 7:37 pm

[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


Jan
Mar 16 2012 | 10:03 pm

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


sxa
Apr 20 2012 | 10:11 am

>>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 | 5:26 am

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.

Feb 28 2014 | 8:48 am

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

Is anyone else seeing that?

May 10 2015 | 1:16 pm

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

May 10 2015 | 1:18 pm

I found this https://hallluke.wordpress.com/2010/10/31/including-extra-javascript-files/

/*
* 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<levels; i++) {
            target = target.slice(0,target.lastIndexOf("/",target.length-2));
        }
        target += name;
    }
    var f = new File(target,"read","TEXT");
    f.open();
    if (f.isopen) {
        while(f.position<f.eof) {
            mem += f.readline(800)+"\n";
        }
        f.close()
    } else {
        var er_file = target.slice(target.lastIndexOf("/",target.length-2)+1);
        var er_path = target.slice(0,target.lastIndexOf("/",target.length-2)+1);
        post("Error importing file \""+er_file+"\" from \""+er_path+"\"\n");
    }
    scope.eval(mem);
}
 
// EOF
May 10 2015 | 1:32 pm

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

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

Forums > Javascript