enumerating chains in javascript

Learn about building and using Max for Live devices.
Post Reply
barstu
Posts: 307
Joined: Fri Oct 20, 2006 4:45 pm
Location: London

enumerating chains in javascript

Post by barstu » Sun May 29, 2011 11:10 am

I'm trying to do a for loop in javascript through every chain in an instrument rack using this:

x = new LiveAPI("id " + rID);
for (var chain in x.get("chains")) {

}

This isn't working because x.get("chains") returns "id 12 id 27 id 42"

So must I do some string processing and remove the 'id' and manually split the numbers into an array myself or is there some inbuilt method to enable me to do a for loop 'out of the box'?

darth_fader
Posts: 36
Joined: Thu Jun 18, 2009 11:29 am
Location: Germany
Contact:

Re: enumerating chains in javascript

Post by darth_fader » Mon May 30, 2011 11:06 am

For components I use the following code. I have not tested it but I guess it works the same for chains:

var api = new LiveAPI (null, "");
var count = api.getcount ('components');
for (var j = 0; j < count; j++)
{
api.path = ['control_surfaces', i, 'components', j];
componentNames[j] = api.get ('name');
}

ST8
Posts: 259
Joined: Mon Jan 26, 2009 12:55 pm

Re: enumerating chains in javascript

Post by ST8 » Tue May 31, 2011 11:41 am

Yep i do the same thing in kapture. Get the number of chains from the api with getcount, then iterate through each one.

barstu
Posts: 307
Joined: Fri Oct 20, 2006 4:45 pm
Location: London

Re: enumerating chains in javascript

Post by barstu » Wed Jun 01, 2011 12:06 am

Thanks for that, As I'm dealing with some deep nested chains I've tried to avoid paths and just stick to ID's using the following

for (var chainID in x.get("chains").asAPIObjects() ){
var chain = new LiveAPI("id " + chainID);
}

Array.prototype.asAPIObjects=function(){
var output = new Object()
for(var c = 0; c < this.length; c++){
if (this[c] !="id"){
output[this[c]] = new Object();
}
}
return output;
}

What I really wanted to do was have the extended function return a collection of the chains as API objects (as the method name suggests) but I couldn't get it working. Ah well the above works ok for me for now.

Thanks for tips

ST8
Posts: 259
Joined: Mon Jan 26, 2009 12:55 pm

Re: enumerating chains in javascript

Post by ST8 » Fri Jun 03, 2011 5:25 pm

Thats a great idea barstu. In python thats how it works, for example tracks returns a tuple of track objects. Would be much simpler in m4l if we could prototype each of the arrays to return the actual objects

Post Reply