get values of number boxes (and other objects)

Peter Nyboer's icon

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.

Wesley Smith's icon

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

Max Patch
Copy patch and select New From Clipboard in Max.

-------------------clipping: gathermatrices.pat

---------------

---------------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]);
}
}

Mattijs's icon

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

Anthony Palomba's icon

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

Anthony Palomba's icon
Mattijs's icon

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

No.. no better way.