get existing subpatcher and put objects in it with js

tdaeik's icon
Max Patch
Copy patch and select New From Clipboard in Max.

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:

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

Luke Hall's icon

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

lh

tdaeik's icon

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

dagfooyo's icon

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!

dagfooyo's icon

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");

Luke Hall's icon

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");

dagfooyo's icon

Aha! Thanks Luke, that totally worked!