Syntax for moving an object in JS

joshua cook's icon

Anyone know the syntax for moving a created object?

I am looking to replace a thispatcher move along the lines of:

script move var1 300 300

Emmanuel Jourdan's icon

On 11 janv. 09, at 00:00, joshua cook wrote:

> Anyone know the syntax for moving a created object?
>
> I am looking to replace a thispatcher move along the lines of:
>
> script move var1 300 300

something like:

this.patcher.message("script", "move", "var1", 300, 300);

should do it. You can also use the rect property of a Maxobj, after
getting the Maxobj with this.patcher.getnamed("var1").

HTH,
ej

joshua cook's icon

I guess I was originally looking for some sort of built in move function as existed in scripting via thispatcher.

I think that the way to do it is to update the @patching_rect atrribute.

joshua cook's icon

EJ - thanks for your quick reply. I also looked at an earlier post of yours for a few pointers and it was very helpful.

So I am trying this:

function move(var1)
{
var obj = this.patcher.getnamed(var1);
obj.message("@patching_rect", 300, 300, 100, 20);
}

I get the message:

patcher: doesn't understand "@patching_rect"

Any suggestions?

Emmanuel Jourdan's icon

On 11 janv. 09, at 00:37, joshua cook wrote:

> So I am trying this:
>
> function move(var1)
> {
> var obj = this.patcher.getnamed(var1);
> obj.message("@patching_rect", 300, 300, 100, 20);
> }
>
>
> I get the message:
>
> patcher: doesn't understand "@patching_rect"

@patching_rect is when you need to generate the object via scripting,
or write the name in an object box. The name of the attribute is in
fact "patching_rect". so:

function move(var1)
{
    var obj = this.patcher.getnamed(var1);
    obj.message("patching_rect", 300, 300, 100, 20);
}

Although if you only want to change the position you can use the
"patching_position" attribute instead so you don't have to know the
size of the object (there's also a "patching_size"):

function move(var1)
{
    var obj = this.patcher.getnamed(var1);
    obj.message("patching_position", 300, 300);
}

The same thing also apply to presentation if you use the
"presentation_rect", "presentation_position" or "presentation_size".

HTH,
ej

joshua cook's icon

Again, EJ, thanks for your quick responses. You are very helpful, especially since JS is not very well documented.

So I have modified your suggested function slightly as below. The object I am moving is a metro.

function move(var1)
{
var obj = this.patcher.getnamed(var1);
obj.message("patching_position", 300, 300);
obj.message("interval", 700);
post("voila");
}

I have modified the function slightly as above. Upon completion, the interval is updated to "700 and "voila" is posted to the max window, but the metro remains in the same place.

Incidentally, I have also been trying to use the rect function and can not get a handle on the syntax. Might you be able to shed some light on how to use this built-in function.

Much obliged,

joshua

Emmanuel Jourdan's icon

On 11 janv. 09, at 01:10, joshua cook wrote:

> function move(var1)
> {
> var obj = this.patcher.getnamed(var1);
> obj.message("patching_position", 300, 300);
> obj.message("interval", 700);
> post("voila");
> }
>
> I have modified the function slightly as above. Upon completion, the
> interval is updated to "700 and "voila" is posted to the max window,
> but the metro remains in the same place.
>
> Incidentally, I have also been trying to use the rect function and
> can not get a handle on the syntax. Might you be able to shed some
> light on how to use this built-in function.

Right. Sorry I forgot to mention that it sending patching_rect and co
only works directly for UI objects. For non-UI object such as metro,
you need to use the "sendbox" message to send the attributes directly
to the box (the UI representation of the object) and not the object
(the thing which "does the real work").

function move(var1)
{
    var obj = this.patcher.getnamed(var1);
    obj.message("sendbox", "patching_position", 300, 300);
    obj.message("interval", 700);
    post("voila");
}

ej

joshua cook's icon

Hi ej

Hope I'm not bugging you.

I guess I should stop being coy. What I am really trying to do is move a bpatcher. Everything you have suggested so far has worked perfectly. Using the function you suggested, I am able to move a metro around. I am even able to move a bpatcher around ... if that bpatcher does not have an assigned patcher file.

Once I assign a bpatcher a patcher file, when I attempt to move it, I get the message:

patcher: "doesn't understand sendbox"

Do you know what is happening? Should I be using rect?

Many thanks,

joshua

Emmanuel Jourdan's icon

On 11 janv. 09, at 01:42, joshua cook wrote:

> I guess I should stop being coy. What I am really trying to do is
> move a bpatcher. Everything you have suggested so far has worked
> perfectly. Using the function you suggested, I am able to move a
> metro around. I am even able to move a bpatcher around ... if that
> bpatcher does not have an assigned patcher file.
>
> Once I assign a bpatcher a patcher file, when I attempt to move it,
> I get the message:
>
> patcher: "doesn't understand sendbox"
>
> Do you know what is happening? Should I be using rect?

You're out of luck ;-) Bpatcher is the special case. The only current
way to send a message to the bpatcher itself is to use the "sendbox"
message via thispatcher:

    this.patcher.message("script", "sendbox", var1, "patching_position",
Math.random()*200, Math.random()*200);

No more special cases. You should be fine now.

ej

joshua cook's icon

bingo. thanks for the lesson.

zoomak's icon

hello all,
thank you also for the lesson,
what about "hide" property, the first way works :
this.patcher.message("script", "hide", "var1");

but the second one fails :
var obj = this.patcher.getnamed(var1);
obj.message("sendbox","hide");

I get "don't understand "hide" " from the object in the max window

zoomak

Emmanuel Jourdan's icon

zoomak wrote on Wed, 11 March 2009 01:07hello all,
but the second one fails :
var obj = this.patcher.getnamed(var1);
obj.message("sendbox","hide");

I get "don't understand "hide" " from the object in the max window

hide is not a message of your object. hidden is an attribute of any object so you should be able to do something like this:

var obj = this.patcher.getnamed("var1");
obj.hidden = true;

HTH,

BenCello's icon

Emmanuel Jourdan wrote on Sun, 11 January 2009 02:07
You're out of luck Bpatcher is the special case. The only current
way to send a message to the bpatcher itself is to use the "sendbox"
message via thispatcher:

    this.patcher.message("script", "sendbox", var1, "patching_position",
Math.random()*200, Math.random()*200);

No more special cases. You should be fine now.

ej

Hi !

Is this still up to date ? There really is no other way to change the presentation_rect (or other presentation attributes) of a bpatcher ?
Because this requires the bpatcher to be named...

Thanx

BenCello

Emmanuel Jourdan's icon

This is the way to go.

Peter Nyboer's icon

how does one get the presentation_rect of an object?

var ob = this.patcher.getnamed("toto");
var loc = ob.presentation_rect;

doesn't work.

Luke Hall's icon

This should do it.

var ob = this.patcher.getnamed("toto");
var loc = ob.getattr("presentation_rect");

lh

Peter Nyboer's icon

odd - when I do that gettattr on a bpatcher with something loaded, I get a "jsobject" in place of actual, useful coordinates.

//begin junk.js

function anything(){
var mypanel = this.patcher.getnamed(messagename);
post("n**name",messagename);
post("nrect",mypanel.rect);
post("npres",mypanel.getattr("presentation_rect"));
var myloc = mypanel.getattr("presentation_rect");
post("nloc",myloc);
}

//end junk.js

Max Patch
Copy patch and select New From Clipboard in Max.

Luke Hall's icon

The code below will report the patching-rect when in patching mode and the presentation_rect when in presentation mode. Hopefully you can make use of this.

lh

function anything() {
    obj = this.patcher.getnamed(messagename);
    loc = obj.rect;
    outlet (0,loc);
}

Peter Nyboer's icon

Yeah, that's what I'm using now, it's just that, for whatever reason, I'm getting very odd results in my patch. Even though the patch is set to be in presentation, the js is reading the rect property of patching mode, not presentation mode. So I had hoped to get the presentation_rect, but then I find that gives weird results with bpatchers! For now I set the patching and presentation modes to work, but I guess I'll have to take this up with the beta list.
Pete.

Luke Hall's icon

The beta list? Sorry I couldn't be of any more help. The [bpatcher] object has always been a little tricky when it comes to scripting. If you do discover a way could you post it here? I'd be grateful.

lh

Peter Nyboer's icon

yeah, If i get some answers on my issues, I'll post the solutions for sure...thanks for the hints so far, tho!

Matthew Aidekman's icon

Any update on programattically getting and setting the presentation rect of bpatchers???

DRKOSS's icon

Perhaps a bit OT, but this is a lot of good info here. What is the syntax for saving the patcher?

I was guessing:

this.patcher.message("script","write");

but no luck.

thanks!

DRKOSS's icon

@EMMANUEL - hoping you can chime on this. thanks!

Emmanuel Jourdan's icon

this.patcher.write() should work.

DRKOSS's icon

@EMMANUEL aha - cheers!

DRKOSS's icon

@EMMANUEL - any way of getting that to work in notifydelteted() function? Trying to figure out a way to save the patch automatically when closed.

this doesn't seem to work in javascript:

function notifydeleted(){
    this.patcher.write();
}

Max Patch
Copy patch and select New From Clipboard in Max.

nor does this in a patcher:

any ideas?

thanks!