get scripting name hierarchy of nested bpatchers

joeman's icon

Dear forum,

I am aware of and have many times used this javascript from Luke Hall (thanks!):

outlets=1;

function bang()
{
if(this.patcher.box){
outlet(0, this.patcher.box.varname);
}
}

to get the scripting name of a bpatcher from inside itself.

I'm wondering if it's possible to use a similar method to find out the scripting names of all of the parent bpatchers when dealing with nested bpatchers, each with their own scripting name.

I'm looking for something that will spit out the scripting names in hierarchical order (up or down) so that I can then convert it to this:

top_level::middle_level::bottom_level

where each xxx_level is the scripting name of the bpatcher (from topmost to the current one).

Is it possible with a bit of js code like that above or any other similar (seemingly) simple method?

This is for a large project that includes the ability to dynamically create bpatchers within bpatchers within bpatchers etc and I want to be able to turn any arbitrary nested child bpatcher's active state to pattrstorage on/off which is only possible when using the syntax:

"active top_level::middle_level::bottom_level $1"

Thank you for your help/time!

Joe

dhjdhjdhj's icon

Bump +1

Ben Bracken's icon

You can iterate through the hierarchy by testing parentpatcher.box or something, maybe something like this:

function bang()
{
outlet(0, getPath(this.patcher, this.patcher.box.varname));
}

function getPath(p, path)
{
    if(p.parentpatcher.box)
        {
        path = p.parentpatcher.box.varname + "::" + path;
        return getPath(p.parentpatcher, path)
        }
        else return path
}

joeman's icon

Hi Ben,

Thank you for your reply. And thanks DHJDHJDHJ for the bump.

Ben, your javascript returns a syntax error. I've tried but I'm not able to decipher how to fix it.

When you have a moment, could you please have another look at it or anyone else who is able. I'm sure it's something very simple.

Thank you!

Joe

Ben Bracken's icon

Sounds like you have it in a top patcher. You can add a check to see if the patcher is a top patcher:

function bang()
{
    if(this.patcher.parentpatcher) // make sure the current patcher is not a top level patcher
    {    
        outlet(0, getPath(this.patcher, this.patcher.box.varname));
    }
}

function getPath(p, path)
{
    if(p.parentpatcher.box)
        {
            path = p.parentpatcher.box.varname + "::" + path;
            return getPath(p.parentpatcher, path)
        }
    else return path
}

joeman's icon

Hi Ben,

Thanks for the updated js but the bpatcher where the js is located is three levels (bpatchers) deep, it's not the top level patcher.

When instantiating the bpatcher the js syntax error in max window reads:

Javascript SyntaxError: illegal character, line 13
source line: path = p.parentpatcher.box.varname + "::" + path;

I also get: "js: no function bang" when sending the js a bang.

Out of interest, when I try loading the same js into a top level patcher, it also returns the exact same errors (using the new js you just posted).

Thanks heaps for your help.

Joe

Ben Bracken's icon

Hi Joe,

No idea what is going on there. if there is no function bang(), then it doesn't sounds like the script was pasted in correctly. There is definitely a function called bang :)

-Ben

joeman's icon

Hi Ben,

Yeah. It's very strange. Definitely pasted in correctly, appears identically.. Definitely not working though. Other js I have with function bang are currently working so will compare them later today when I get another chance.

Joe

joeman's icon

SOLVED

Thanks so much Ben. Your javascript works a treat for my purposes and you've been super helpful.

FYI, the problem I was having with the pasted javascript had to do with the inverted commas around the colons ("::"). They must have been formatted strangely from the forum, the looked to be in italics (or similar) and so I replaced the ones in the js by typing them in manually. The freshly typed inverted commas solved the problem I was having and all works as it should now.

Cheers!!

:-)

Joe