Forums > Javascript

get values of number boxes (and other objects)

October 27, 2006 | 10:11 pm

I feel like I’m missing something obvious, but is it possible to retrieve a value of a max object in a patch with js? I don’t see any documentation or examples of this. I’m looking to do something like:
var obj = this.patcher.getnamed("mynumberobject");
var myvar = mynumberobject.value;

where "mynumberobject" is the scripting name of a number box.
Looking at the docs, it’s clear that "value" isn’t a Maxobj property, nor is there a "get" method. How would one do this?

Peter.


October 28, 2006 | 12:13 am

Indeed there is. It’s probably not documented because there’s no JS
function for it per se, but you can build your own. Below is an
example of how to retrieve the names of all matrices in a patch. The
basic technique is to iterate through patcher objects and use the JS
functions connect()/disconnect() to invisibly talk to patcher objects
without a user ever seeing actual patch coords.

wes

——————-clipping: gathermatrices.pat
max v2;
#N vpatcher 44 104 516 403;
#P button 98 24 15 0;
#P user jit.pwindow 59 139 82 62 0 1 0 0 1 0;
#P window setfont "Sans Serif" 9.;
#P message 177 84 88 196617 exportimage jpeg;
#P newex 143 84 27 196617 t b l;
#P newex 143 111 55 196617 jit.matrix;
#P user ubumenu 98 64 100 196617 0 1 1 0;
#X add;
#X prefix_set 1 0 name 0;
#X pattrmode 1;
#P newex 98 43 116 196617 js xray.gathernames.js;
#P bpatcher 143 140 262 78 0 0 tl.jit.info.pat 1;
#P comment 117 25 52 196617 < = bang it;
#P fasten 4 0 7 0 148 133 65 133;
#P connect 8 0 2 0;
#P connect 2 0 3 0;
#P connect 3 1 5 0;
#P connect 5 0 4 0;
#P fasten 5 1 4 0 165 105 148 105;
#P fasten 6 0 4 0 182 105 148 105;
#P connect 4 0 1 0;
#P pop;

—————

—————js: xray.gathernames.js——-
var p = this.patcher;
var obj;
var matrixNames = new Array();
var counter = 0;
var self = this.box;

function bang()
{
counter = 0;
obj = p.firstobject;

//limit search to first 100 objects
while(obj && counter < 100)
{
if(obj.maxclass == "jit.matrix")
{
p.connect(obj, 1, self, 0);
obj.message("getname");
p.disconnect(obj, 1, self, 0);
}

obj = obj.nextobject;
counter++;
}

fillmenu();
counter = 0;
}

function name(matrixname)
{
if(! alreadyexists(matrixname))
matrixNames[matrixNames.length] = matrixname;
}

function alreadyexists(matrixname)
{
var exists = 0;

for(i=0; i < matrixNames.length; i++)
{
if(matrixNames[i] == matrixname)
{
exists = 1;
}
}

return exists;
}

function fillmenu()
{
outlet(0, "clear");

//make ubumenu prepend
outlet(0, "prefix_mode", 1);
outlet(0, "prefix", "name");

for(i=0; i < matrixNames.length; i++)
{
outlet(0, "append", matrixNames[i]);
}
}


October 29, 2006 | 4:40 pm

Nice, I just had the same question.

Wes, your solution is a nice workaround but as far as I can see it has two drawbacks: 1) when sending the message getname, the name is also ouput in the max patch. 2) this only works when the object who’s info you need is in the same patch

How about a method that, like [grab], retreives the results of a message? Something like this:

var name = myMaxObj.grab("getname", 2); // second argument is the outlet to retrieve


May 31, 2008 | 6:47 pm

So is there a better way to do this now? I want to query the
value of an object from js…


June 2, 2008 | 1:39 pm

Anybody?


June 2, 2008 | 3:23 pm

Quote: Anthony Palomba wrote on Mon, 02 June 2008 15:39
—————————————————-
> Anybody?
—————————————————-

No.. no better way.


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