Parsing arguments and attributes
Nov 07 2009 | 3:12 am
I've seen this mentioned a few times on the list but have never seen any code that demonstrates it properly (or at least that I could understand). I've been playing with javascript in max quite a lot recently so I thought I'd have a go myself.
First you need to place the lh.jsextensions.js file in your Cycling '74 > jsextensions folder and restart max. Then put the lh.aa_example.js file somewhere in your search path and fire up the max patch. It should demonstrate how you can query arguments and attributes from inside your javascript file.
You need to define a variable and initialise the aa_parser(), making sure you pass it the this keyword in your global code. You can then use the getargs() and getattr() methods to return arrays of the arguments or specified attributes. There is also the query() method to print everything to the max window. Hopefully the patch will make everything clear.
There are two fairly important things to note: attribute names can only contain letters, numbers and the _underscore_ character (although this could easily be changed) and at the moment attributes not followed by regular jsarguments are completely ignored.
If you do have any questions or problems the let me know. I hope someone else finds it useful.
lh
// lh.jsextensions.js /* * argument and attribute parser for javascripts in max * * initial jsarguments become the getargs() array * "@somename" formatted jsarguments define a new attribute * any following jsarguments become the getattr("somename") array * attributes with no following jsarguments are ignored * attribute names must only contain characters: a-z0-9_ * * example: * * var aa = new aa_parser(this); // define variable and initialise * var args = aa.getargs(); // returns array of arguments * var attr = aa.getattr("type"); // returns array of "@type" attribute * aa.query(); // print arguments and attributes to the max window */ function aa_parser(scope) { var flag; var temp = []; var attrnames; var attrvals; var args; var re = /^@w+$/; this.init = function() { flag = 0; args = []; attrnames = []; attrvals = []; for (var i=1; i
// lh.aa_example.js var aa = new aa_parser(this); var args = aa.getargs(); function findattr(a,b) { var attr = aa.getattr(a); outlet(0,attr[b]); } function findarg(a) { outlet(0,args[a]); } function bang() { aa.query(); } autowatch = 1; // EOF