Forums > Javascript

Syntax for moving an object in JS


Jan 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

Jan 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

Jan 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.

Jan 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?

Jan 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

Jan 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

Jan 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

Jan 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

Jan 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

Jan 11 2009 | 1:24 am

bingo. thanks for the lesson.

Mar 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

Mar 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,

Jun 01 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

Jun 01 2009 | 3:56 pm

This is the way to go.

Jul 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.

Jul 10 2009 | 8:40 pm

This should do it.

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

lh

Jul 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. —
Jul 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);
}
Jul 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.

Jul 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

Jul 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!

Nov 05 2009 | 1:23 pm

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

Jun 18 2015 | 5:04 am

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!

Jun 21 2015 | 6:26 pm

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

Jul 08 2015 | 7:58 am

this.patcher.write() should work.

Jul 14 2015 | 5:48 pm

@EMMANUEL aha – cheers!

Aug 12 2015 | 7:45 am

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

nor does this in a patcher:


-- Pasted Max Patch, click to expand. --

any ideas?

thanks!

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

Forums > Javascript