Forums > Javascript

Parsing arguments and attributes

November 7, 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

----------begin_max5_patcher----------
826.3ocyXssbaBCD8Y6uhc34DOHIDW5SteGMYxHLJ1JEj7.hVmlI+6EDfCow
tAguDdgYzp0lyd167x7YNwpc7BG3avOfYydY9rYFQ0Bl0ddlSFa2pTVgQMGI
+2p3mbto4JMem1HdatPpgbdQYpF2cqHwbWk92h85D9nRpKD+gWeEBuvsU7Vl
d0Fgb8C47U5F.4EUcKDRqeFPpehcW3B229KjkYBYJWavE5MgpRcmT2duSIKy
7Nc9dtfk5TewqymW+3lAZ5wkZsR1YGMuF8ya4Mn0IlIW6rGc8scpymYjdnNy
ydiDMBS4HdwmJfzMKXrG36XYaS4fKPfkYpDNjJjbVNr7W77XUAGPGkGNLG3O
V+ueim2OXTbyzH.f7eB.P3FeOw8JGBTlEyyOpwTkN6by+ZSU+AhrxrZU7NfY
tOlnBvc54ZsamP8p4.TCUPGEgfOq98LdQAaM+CoKJ01B6RCPQ1mFPLW2EmPL
4AnviwG3qPdvw3CRdhkzQf8zANvuOcfWPmpzwCE7UJosTBcDTBpOkP8mtQHO
JxKzVRHD6IDTn6DKF4XiMw9Iv.WKYDz3YDulZqQex3TekTRNeKWl.OJjILsN
2Rtwc7biuIwIh90OnwI1al9QdI5rzZNHpWPzTtyLl5aWbyHZDgZlOESLEYBl
.UcGvtYGX0L5XmLucj0wMYt6UuVR9Z6BIFwFqs4GSmBIiciExmtwZqQN8VY8
VDfAxBjGrzLsArgmlpf6bVqTIP7y76bfkh0RUNOAV1NiVUS3XXUk7zaS4qYo
UpWtATap0sZpVnpdBPQX.491Gxndi3UpRote58.6RM1TNTTyDMDS+ah2kH1x
7OZrt+4CCYPUs726kJTk4q5L2tc8g2.VBuPKjLsnJPrmRduSoMhjDtreggLQ
xVUUgqVP.2evXlAiI5Pvj+UESng.IhcPhFVOqKF2rkXUA5tSmJ+MDvVOXJft
d7WzPwjEDHoZmg5sEZ9hbnvn8mNUzFbAPKNjdgPK8RfV7kBsjK.ZOULMnLF2
qKlbGBlrCRctwvPyRAg6ObhXcHI2dSt50A1UA7ckqwH54pb8PfJ8pRddm+Hu
vvKRfG47iz2gnpCuN+u.klVSYA
-----------end_max5_patcher-----------


November 9, 2009 | 2:02 pm

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


November 16, 2009 | 12:15 am

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


February 24, 2010 | 1:42 am

this is interesting. many thanks


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