this.patcher.box and bpatchers

vade's icon

Hello.

I am attempting to create a JS, that when used will discover the absolute position of an named object, regardless of whether its in a batcher, a few bpatchers, or in the parent itself.

In the javascript in max pdf, the thispather.box property is used to return the parent patcher. However, I get errors trying to reference this.patcher.box from within a bpatcher. Does this only work within subpatchers?

A workaround ive found is to use this.patcher.parentpatcher,which seems to resolve parent bpatchers properly. However, it stops once it reaches the patch that contains the bpatcher, (ie, only one level above), and will not iterate to any other parent patchers.

The following code works properly when called from either the main patch, or the parent patch, but no futher above the patcher heirarchy.

I am obviously misunderstanding something. Id love a pointer in the right direction.

function locate(name)
{
    obj = this.patcher.getnamed(name);

    objCoords= obj.rect; // get the cordinates from within the objects patch

    windowCoords = this.patcher.wind.location; //get the window coords

    prev = 0;
    owner = this.patcher.parentpatcher;
    while (owner)
    {
        post(owner.name); //debug
        prev = owner;
        owner = owner.patcher.parentpatcher; //iterate up.
    }
    if (prev)
    {
        post("top patcher is", prev.name); //debug
        windowCoords = prev.wind.location; // set the window coords to the topmost window
    }        
// get our x/y position from window and local coords
    x = windowCoords[0] + objCoords[0];
    y = windowCoords[1] + objCoords[1];

    outlet(0, x,y+13, x+126, y+73);
//    post();
//    post(x, y);
}

Thanks,

vade's icon

So, there is no way to get these coordinates, or, is there a way to get the asbolute screen coordinates of any arbitrary named object?

If I have something like

/main patch/bpatcher/bpatcher/bpatcher/named item

and I want to get the global (or hell relative from main patch) location of 'named item', with arbitrary amount of nested bpatchers, I cant?

Anyone want to be my personal hero?

Jeremy's icon

Would you post a complete example, please? Your email is slightly, er, difficult to comprehend, in terms of mocking up something to test and/or fix. I'd like:

A patch in which the js works
A patch in which the js does not work

with the JS

in a folder

in an archive

on a website (or in my mailbox)

please
jb

Wesley Smith's icon

Hi Vade,
I've attached a zip file with a JS, 2 bpatchers, and a main patch.
One of the bpatchers has a nested bpatcher as well. The patch shows
how to get absolute coordinates of max objects in batchers. This is
all using the maxobject.rect property. Hopefully the attachment gets
posted. If not, I'll repost it tommorrow when I can get my computer
back online.

best,
wes

Wesley Smith's icon

Ok, so I don;t think the attachment went through. Here's a link to the file:
http://www.mat.ucsb.edu/~whsmith/JS_bpatcher.zip

On the matter of 'having to find the parent window to get global
coordinates'. I think this patch pretty clearly demonstrates that
this is not the case. I never get the coordinates of the windows and
am perfectly capable of getting the location of a double nested object
(located in a bpatcher in a bpatcher) with the rect property. Now, if
you want to know where it is in screen coordinates, you will have to
get the x, y location of the main window, but *only* the main window.
This is not such a big deal as it is trivial to access the main window
that the JS object is located in.

wes