Max-MSP Javascript Cheatsheet

Here’s a small, illustrated, collection of properties, function and keywords that are really useful when working with js (or jsui) objects in Max.

Use the following expressions in the global section of you script (technically, these are properties of the jsthis object) :

// automatically reload the script when the js file is modified.
autowatch = 1;

// define the number of inlets / outlets
inlets = 2;
outlets = 3;

For a complete listing of the jsthis properties and methods, please refer to these pages : JS globals in Max 5, JS reference for Max 6. There you will meet some wonderful friends like box, max, patcher, assist or declareattribute…

Use these properties to alter the functions you create :

function myFn() { // do something here
}

// make myFn private
myFn.local = 1;

// make myFn run in the high priority thread
// (but beware of side effects)
myFn.immediate = 1;

When communicating with other MaxObjects, the following functions have pre-defined behaviours :

function loadbang() { // called when the patch is loaded
}

function bang() { // called when receiving a bang
}

function msg_int(v) { // if defined, this function is called when receiving an int value
}

function msg_float(v) { // called when receiving a float value // (if not defined, a float will be truncated and msg_int will be called)
}

function list() { // called when receiving a list // use the arguments object to handle a arbitary number of arguments post(“received a list of “ + arguments.length + “ values”);
}

function anything() { // this function will catch anything that the above functions didn’t // notice the use of the messagename and inlet properties post(“Got a message named : “ + messagename); post(“ arriving via inlet #” + inlet);
}

Code snippet to get the size of a jsui object (using the box property of the jsthis object)

var width = box.rect[2] — box.rect[0];
var height = box.rect[3] — box.rect[1];

Code snippet to create tooltips on outlets. Passing -1 as first parameter allows to define a generic function used for all outlets.

function assist(num)
{ assist(“this is outlet number”,num);
}

setoutletassist(-1,assist);

Also available for inlets as setinletassist.

Stay tuned for some more…

Written by

Doing stuff with the @herrmutt Lobby collective. Also 3D french fries artist.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store