Forums > Javascript

get existing subpatcher and put objects in it with js

December 20, 2009 | 3:25 am

hi, i want to get the subpatcher in an existing patch and place objects in it using java script, but i cant seem to. i have the following max patch:

– Pasted Max Patch, click to expand. –

and the following java script:

// inlets and outlets
inlets = 1;
outlets = 2;

// global variables and arrays
var numenvs = 0;
var pxenvs;

function intresp(a) {
numenvs=a;
pxenvs=this.patcher.getnamed("xenvs")
post(pxenvs.name)
}

eventually i want to place new objects in the xenvs subpatcher, but this.patcher.getnamed("xenvs") returns nothing, i get an error:

js: make_envs.js: Javascript TypeError: pxenvs has no properties, line 12
js: error calling function intresp

can someone help?

many thanks,,


December 20, 2009 | 5:55 am

The getnamed() function finds objects by scripting name, so you need to set this for your [patcher] using the inspector.

lh


December 20, 2009 | 8:23 am

hi luke,
thanks so much. also had to use the subpatcher() function but works as i wish,
cheers,,


January 21, 2010 | 10:08 pm

Could someone please write out the exact syntax for accessing a subpatcher in javascript? I’ve tried using commands with every combination of subpatcher() and getnamed() I can think of and I just haven’t been able to get it to recognize the subpatcher at all.

Just an example command of how you would instantiate a new object inside a subpatcher with javascript would be perfect. I can’t believe there’s nothing in the documentation about this.

Thanks!


January 21, 2010 | 10:24 pm

This is the js code I have so far, currently what it does is create the new object in the main patcher, not in the subpatcher. Seems to do that, or else give an error. What am I doing wrong?

var blahdeblah = this.patcher.newdefault(100,100,"p", "blahdeblah");
blahdeblah.varname= "blahdeblah1";

var test = this.patcher.getnamed("blahdeblah1").patcher.newdefault( 100, 100, "js", "chanController.js");


January 21, 2010 | 10:55 pm

If you want to create an actual [patcher] object then you can use the first block of code below. You were pretty close. Firstly you don’t need to use getnamed() when you’ve already used a variable when you create the object. The main problem is the second use of patcher, this should be subpatcher() instead which reports the pathcher object of a maxobj, bit difficult to wrap your head around but it’s all in the javascript documentation. If you want to create a new patcher on its own then you can use the second piece of code.

lh

var pee = this.patcher.newdefault(100,100,"p");
pee.subpatcher().newdefault(100,100,"toggle");

var p = new Patcher(300,100,700,400);
p.message("front");
p.newdefault(100,100,"toggle");


January 21, 2010 | 11:56 pm

Aha! Thanks Luke, that totally worked!


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