get existing subpatcher and put objects in it with js

Dec 20, 2009 at 3:25am

get existing subpatcher and put objects in it with js

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) {

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

Dec 20, 2009 at 5:55am

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


Dec 20, 2009 at 8:23am

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

Jan 21, 2010 at 10:08pm

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.


Jan 21, 2010 at 10:24pm

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

Jan 21, 2010 at 10:55pm

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.


var pee = this.patcher.newdefault(100,100,”p”);

var p = new Patcher(300,100,700,400);

Jan 21, 2010 at 11:56pm

Aha! Thanks Luke, that totally worked!


You must be logged in to reply to this topic.