Forums > Javascript

Syntax for moving an object in JS

January 10, 2009 | 11:00 pm

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


January 10, 2009 | 11:13 pm

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


January 10, 2009 | 11:20 pm

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.


January 10, 2009 | 11:37 pm

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?


January 10, 2009 | 11:55 pm

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


January 11, 2009 | 12:10 am

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


January 11, 2009 | 12:23 am

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


January 11, 2009 | 12:42 am

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


January 11, 2009 | 1:07 am

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


January 11, 2009 | 1:24 am

bingo. thanks for the lesson.


March 11, 2009 | 12:07 am

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


March 11, 2009 | 2:15 am
zoomak wrote on Wed, 11 March 2009 01:07
hello 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,


June 1, 2009 | 2:56 pm
Emmanuel Jourdan wrote on Sun, 11 January 2009 02:07
You’re out of luck Wink 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


June 1, 2009 | 3:56 pm

This is the way to go.


July 10, 2009 | 5:51 pm

how does one get the presentation_rect of an object?

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

doesn’t work.


July 10, 2009 | 8:40 pm

This should do it.

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

lh


July 13, 2009 | 10:46 pm

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

– Pasted Max Patch, click to expand. –

July 14, 2009 | 12:36 am

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

July 14, 2009 | 1:39 am

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.


July 14, 2009 | 1:42 am

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


July 14, 2009 | 1:48 am

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


November 5, 2009 | 1:23 pm

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


Viewing 22 posts - 1 through 22 (of 22 total)