Stupid JS Tricks

It doesn’t have quite the same ring as Letterman’s “Stupid Pet Tricks” but you get the point… This hopefully periodic new column will attempt to get Max’s JavaScript object, JS, to jump through a few hoops and garner our applause. The idea is similar to Andrew Benson’s excellent Jitter Recipe’s. So let’s get started…

Stupid JS Trick #1: Resizing Your Abstraction’s Patcher Interface

The method discussed here for resizing patcher windows and bpatcher boxes is used in the Jamoma project. This screenshot shows the same patcher (Jamoma’s limiter~ module) loaded as a plain object and in a bpatcher.

Here’s the challenge: we want to create an abstraction (a Max patch that is loaded as an object in another patch). When the user double-clicks our object, it should open an interface window. Rather than having to remember to manually resize the window tediously every time we save the abstraction, we want to have it happen automatically. And just to make things more interesting, let’s say that we want to have this little JS gizmo inside of another abstraction inside of the abstraction.

After mulling this over briefly (all of the functions are documented in the “Javascript in Max” PDF that is in the Max documentation), we can make a couple of improvements. Namely, what happens if a user loads our abstraction in a bpatcher? Ideally, we want the bpatcher to be automatically sized too. How do we do that?

The code below is an amended version of what we did above. We test to see if we are in a bpatcher using a questionable method where we see how high the object box is, and assume it is not a normal object if the height is more than 50 pixels. Unless you use a really big font in your Max objects, or your bpatcher is really short, then this should work.