14 March 2011

Max/MSP Javascript: Dollar Function - Returns a reference to a Max Object in a patch/subpatch

I started using Max this month and was simultaneously impressed with its power and simplicity and frustrated by it's cumbersome interface. I quickly checked out it's javascript capabilities and became much more comfortable. JS allows you do simple operations quite quickly that would take a tonne of parsing objects which use up space and lead to a messy patch.

That said, JS in Max is really a slightly ugly cousin of browser based javascript. Calls to objects are long winded and basically everything is a bit head wrecking!

So here we go: the first in my "Making Max JS less crap" series... The infamous dollar function! All you need to do to access a max object is give the object a scripting name in the inspector* and then use: $('scriptingname')

Please see right hand pane before copying my code>>

function $(stringref){
    stringref=stringref.replace(/parent/gi, "parentpatcher")
    var path=stringref.split('.')
    var obj=this.patcher
    for(i in path){
        if(path[i]=='parentpatcher'){ //up 1 level:
            if(i!=path.length-1){ //down 1 level:

$("myButton").message('set','new text!') //access 'myButton' object in current patcher
$("parent.myButton").message('set','new text!') //access 'myButton' object in parent patcher
$("myPatcher.myButton").message('set','new text!') //access 'myButton' object in child patcher
$("parent.myPatcher.myButton").message('set','new text!') //access 'myButton' object in sibling patcher

* If you're not bothered giving every patcher a unique scripting name in the inspector, try my lazy dollar function instead.

A good way to use this function is to use Luke Hall's include script. Save his script into your jsexternals folder, save my functions into your project folder as basefunctions.js (or something similar) and then you can simply use include(this,"basefunctions.js") at the top of every JS file you use. Thanks Luke - this is awesome!


Anonymous said...

Just a quick mention of the "jsextensions" folder in the Max search path. It's the best home for these types of utility functions as you can then use them in any other javascripts you write.

cron said...

BUG: After some testing it turns out there are some problems calling this function in a for/while loop. Max seems to be mildly retarded when it comes to JS and the speed of the for loop is too fast for the function (wtf?!) and the program crashes like a big bastard.

Anonymous said...

This was helpful. I have prototypes on my array objects so I needed to change the loop invariant to explicitly run for the length of the split path.

Meiqing Xu said...

cheap jordans
louis vuitton handbags
cheap nfl jerseys
supra for sale
christian louboutin shoes
fitflops sale clearance
michael kors outlet
toms shoes
lebron james shoes

spencermeyer said...

In the long run, a bag tackk.com/ can have a zillion bells as well as Check This Out whistles however if it doesn't swallow you up like Leading 8 Finest Blender For Smoothies Or Food Processor Mixer Combos your preferred down comforter if it does not keep.

JulieAzevedo said...

Cordless as well as electric sites.google.com/ blowers will commonly clear up Title to six to ten inches of snow, yet you may have to work to get Snow Blower Buying Guide 2017 them through much heavier snowfalls.

Adiba Alam said...

With this adjustment in the market, the Web Developers should take up appropriate preparing to keep them refreshed and sparkle in the Web Development field. css

mary Brown said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training in India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

柯飞敏 said...

christian louboutin
oakley sunglasses wholesale
kd 10 elite
ray ban sunglasses outlet
pandora charms
true religion jeans sale
ralph lauren outlet
canada goose jackets
ugg boots on sale
chloe sunglassess

chenlili said...

jordan 8
balenciaga triple s
givenchy handbags
parajumpers jackets
ralph lauren
new balance shoes
nike air max 1
visconti pens
lebron soldier 11
giuseppe zanotti

chenmeinv0 said...

adidas superstar
longchamp handbags
true religion
abercrombie and fitch
converse outlet store
coach outlet
seahawks jerseys
salomon shoes
ai max
ray ban sunglasses

sudha P said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
Java training in Chennai

Java training in Bangalore

sathish said...

I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!

Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

jorick228 said...

Played on BGAOC with big wins? NOT? Come to us as soon as possible and win with us. perfect all slots casino Come get your chance to win.

service care said...

I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating for us.
oneplus service centre chennai
oneplus service centre
oneplus mobile service center in chennai

Post a Comment

Cron Design Studio: Dublin based web design & software development