ClojureScript for MaxMSP
Max Clojure JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



JSUI graphics in ClojureScript.


This is a small collection of examples of some simple MaxMSP JSUI graphics scripting where the Javascript is generated using ClojureScript:


To build the example, install Leiningen and the cljsbuild plugin. Then:

$ lein deps
$ lein cljsbuild once dev
$ lein cljsbuild once prod

There are two build targets: dev runs the Google Closure compiler backend in minimal optimisation mode, whereas prod turns on further optimisation for "production" output. (This is what Google Closure refers to as "simple" optimisation; the full-blown "advanced" mode seems to be throwing up code generation errors in the ClojureScript libraries.)

The dev mode compiles faster, but prod is fast enough to be usable since we've taken out the "advanced" optimisation mode (which is slow).

The output of dev, in this project, is _main-dev.js; the prod target writes _main.js. Once one of these is built, you can open any of the patchers in jsui-cljs.maxproj in Max 6 to see the result. (Any non-msgraphics code would probably run in Max 5, but you'd need to fix up the search path and load the patcher files manually, and Max 5's Javascript engine is much slower than that in Max 6.)

All the patcher files load the same Javascript file: the code executed is determined by the JSUI argument. Look at core to see the selection on the ClojureScript side.

For general development, consider running

$ lein cljsbuild auto dev

(or prod) in the background: this automatically recompiles whenever any (ClojureScript) sources are changed. If the source sets autowatch to 1 (as in our examples), then changes to sources will automatically be recompiled and reloaded into Max.


The example code is documented here (using Marginalia). To build the documentation:

$ lein marg src-cljs/


"Advanced" optimisation in Google Closure merits some discussion. One of the optimisations munges variable names, to make them shorter and more efficient. We need to prevent munging of names we're using implicitly from the Max world (mgraphics, autowatch etc.), and also make sure that we can plant names for JSUI to use which themselves won't get munged. There's more detail here, but the upshot is that we need to provide a mock externals file declaring stubs which shadow the things we need, or export, at the Javascript level. We have one in src-cljs/externs.js, although it's currently far from complete.

(For "simple" optimisation, there's no top-level variable renaming.)


Copyright © 2012 Nick Rothwell,

Distributed under the Eclipse Public License, the same as Clojure.