Forums > Javascript

bpatcher embedding itself automatically AFTER creation

February 4, 2013 | 11:11 am

Hi,

I am quite new to JS in Max and have a hard time figuring out what is possible and what not (if there is anything similar to a JavaDoc floating about, could anyone please point me to it since I don’t seem to be able to find it… thanks!)

In any case, I want make an abstraction, that is called by a bpatcher make the bpatcher embed itself into the parent patch. That is, I am trying to tick the ckechmark for "Embed Patcher in Parent" in the inspector automatically.

The problem is, I have no thispatcher in the parent patch I could send a message to, and I cannot give the bpatcher a scripting name to send it via sendbox (if I would get to that point, I could tick the checkmark by hand already anyway and there is no need for my question here).

The reason I am posting this question in JS is, that I am aware that you can retrieve and change the size of the bpatcher window by calling "this.patcher.box.rect;" in JS and assigning a new value to it. Now, I am wondering if there is any similar way to tick the checkmark, but I don’t know if there is a similar property to access. Any way to change the XML structure of the maxpat file perhaps?

Thanks in advance!


February 5, 2013 | 11:02 am

There seems to be a method called getattr which I can call on this.patcher and retrieve "bgcolor" (i.e. bgcol = this.patcher.getattr("bgcolor"); ) for example. Trying to retrieve the attribute "embed" though only returns me jsobject0… If anyone has a hint, please let me know, cheers!


February 5, 2013 | 12:19 pm

Okay, I was stupid enough not to find the Javascript reference in the Max Help (sorry for being impatient!): http://cycling74.com/docs/max5/vignettes/js/javascriptinmax.html
Also I found this nicely generated JavaDoc style reference of Javascript in Max: http://www.tim-schenk.de/max-javascript-reference/#gsc.tab=0

Now, these references helped me find a way to rename the scripting name of the recently created bpatcher and modify its "embed" attribute using the sendbox in the parent patcher. Quite the hack, but perhaps its useful to others:

//this function should be called WITHIN the bpatcher that you want to embed into its parent
//it is assumed that you call it in the loaded abstraction using a loadbang (loadmess) object, hence, the bpatcher should be the last object created at the time of execution of this method
// also, be sure to prevent further loadbangs from happening after its creation, or else you will run into problems

function embed_unnamed() //auto-embed a bpatcher with no scripting name
{
	varname = "this_is_a_completely_random_name"; //give random name which will be deleted again at the end of this process
	obj = this.patcher.parentpatcher.firstobject;

	while (obj != null) //assume bpatcher was last object added
	{
		last_obj = obj;
		obj = obj.nextobject;
	}

	last_obj.varname = varname; //give bpatcher a random name to refer to it using the sandbox scripting technique
	this.patcher.parentpatcher.message("script", "sendbox", varname, "embed", 1); //embed the bpatcher
	last_obj.varname = ''; //delete the scripting name given
}

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