Parsing arguments and attributes

Luke Hall's icon

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
Max Patch
Copy patch and select New From Clipboard in Max.

Luke Hall's icon

I've fixed this so you don't have to call the methods to return the arrays of arguments/attributes, you can now access them directly with properties.

To use the example I posted previously, the arguments would be aa.args, and the two attributes would be aa.mode and aa.verbose. You can also access these dynamically with aa[string] where the variable string is either "mode", "verbose" or "args". Obviously this means you cannot name one of your attributes "@args", if you do it will be ignored.

This should make attributes much easier to use. It's not quite ready but hopefully I'll post the new code later today.

lh

Luke Hall's icon

Here it is. If anyone does attempt to use it please let me know, it's still a work in progress. Next step is to define allowed attributes and throw errors when something unexpected turns up.

lh

/*
* argument and attribute parser for javascripts in max
*
* initial jsarguments become the args property array
* "@somename" formatted jsarguments define a new attribute
* any following jsarguments become the somename property array
* attributes with no following jsarguments are ignored
* attribute names must only contain characters: a-z0-9_
* "args" is reserved, an attribute named "args" will be ignored
*
* example:
*
* var aa = new aa_parser(this);        // define variable and initialise
* property: aa.args;            // returns array of arguments
* property: aa.type;            // returns array of "@type" attribute
* object: aa["attrname"];        // dynamically return one of the above
*/

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

Griotspeak's icon

this is interesting. many thanks