Recursively searching for named object

Mar 8, 2013 at 1:10am

Recursively searching for named object

I am trying to come up with a js function that will recursively search a patch for a named object…

What I am finding is that I am not able to look inside subpatchers. It looks like when I call firstobject of a patcher object it does not return anything. I have reviewed my code, but I don’t see any obvious problem.

Does firstobject work differently than I am using it? Am I missing something?

  1. objTest.maxpat
Mar 8, 2013 at 4:39am

Hi Anthony,

I haven’t looked closely at your JS code, and I’m sure you have a good reason for doing this via the JS/Max API.
Another way to do it though would be to search the JSON data structure of the main patch and/or other embedded abstraction patches using dict, or using your own JSON code in Javascript (I have a dict-like js object for Max5:


Mar 8, 2013 at 11:06am

Try this one. I still don’t find your object, but it’s iterating properly now. I made a couple of comments in the code.

[attachment removed due to error, see below]

Best, Jeremy

  1. objTest.js
Mar 8, 2013 at 3:19pm

@Zachary the reason why I am doing this in js is because I am developing a 3D scene rendering interface in js. For this task, implementing things in javascript is so much easier than patching things. Of course it requires that I be able to access objects from javascript. Having to search a JSON data structure seems a little convoluted when I have javascript methods that are supposed to traverse the patcher. I will certainly look at the example you posted.

Mar 8, 2013 at 3:35pm

Also, searching the JSON structure directly is not exactly future-proof. We might change the patcher document format again, or start saving patchers in “Copy Compressed” format, or introduce a binary format. Or maybe it’s a M4L device and there’s no valid JSON document available, or, or, or… Anyway, the JS I posted should be appropriate for all anticipated version of Max in which patchers and objects are still part of the landscape.

Best, Jeremy

Mar 8, 2013 at 4:03pm

By the way, the problem my semi-solution JS above is a missing () after the word subpatcher on line 49. The attached version works properly.

  1. objTest.js
Mar 8, 2013 at 4:35pm

Jeremy thanks for your help, it works like a charm. I have been using the following link as documentation
for the Max Javascript API:
I can not find any documentation for subpatcher(). Am I missing something?

Mar 8, 2013 at 4:58pm

Hi, Anthony — it’s in the “The Maxobj Object” JavaScript vignette. Last method from the bottom.


Mar 8, 2013 at 5:09pm


This is much easier to do with the applydeep function:

– Pasted Max Patch, click to expand. –

autowatch = 1;

var scripting_name = null;

function findObject(obj)

if (obj.varname == scripting_name)


function find(_scripting_name)
scripting_name = _scripting_name;

  1. objTest2.js
Mar 8, 2013 at 6:01pm

@Anthony, Jeremy, yes of course you should do this in the officially supported way, if that exists (which it does apparently).

@Jeremy, binary format sounds nice! If for nothing else, would be great to export as standalone using BSON or similar for smaller package size, etc. I know you were just making a point of future-proofing your patches, but anyways…


Mar 8, 2013 at 6:14pm

@Nat thanks for pointing this out. It is indeed easier and works!

Mar 8, 2013 at 6:20pm


How about this one ?

Mar 8, 2013 at 6:23pm

No problem, nevermind my reply then (it was a reply to the message you deleted)


You must be logged in to reply to this topic.