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:
            obj=obj.parentpatcher
        }else{
            obj=obj.getnamed(path[i])
            if(i!=path.length-1){ //down 1 level:
                obj=obj.subpatcher()
            }
        }
    }
    return(obj)
}


//USAGE:
$("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.

UPDATE:
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!


213 comments:

«Oldest   ‹Older   201 – 213 of 213
Pushpalatha said...

This blog is very informative and easy to understand. The demand for data analysts is growing rapidly. Anyone interested in this field should explore How to Become a Data Analyst . It’s very useful. Also, practical training through Data Analytics Course in Coimbatore is important.

Gokul said...

Very informative content about Data Analytics Course in Coimbatore

skyappz said...

Amazing explanation! The guidance provided for Advance AI Course in Coimbatore is practical and beginner-friendly.

skyappz said...

Great article! This platform gives a clear understanding of thetop 10 artificial intelligence colleges in Coimbatore fees and its career benefits.

skyappzdharshan said...

Great blog! It provides valuable insights and information. Thanks for sharing this wonderful content. check this out guys AI / ML training institute

Gokul said...

Wonderful explanation and useful guidance about Learn Full Stack Development and Data Science Course in Coimbatore.

birundhadevi said...


Really helpful content! The platform makes planning feel less stressful. The AI recommendations guide users in the right direction easily. universities without IELTS in Germany


skyappzdharshan said...

This was a fantastic read. The content is detailed enough to be informative while still being easy to follow. I picked up a few useful insights from it living expenses in UK for students per city

Davidprincy said...

Well written! The platform makes the entire process clear and easy to understand. The smart suggestions save time and effort for users. free education countries for indian students

birundhadevi said...


Excellent post! The platform makes everything feel more simple and approachable. It helps users avoid confusion and take the right stepsuniversities in Germany

hazelrae said...

Excellent article with valuable insights. This information is very helpful for anyone looking to start a career in MERN Stack data analytics. Data Analytics Course in Madurai

Muthu Krishnan R SkyAppz said...

Such a great perspective—thanks for sharing this!. high demand courses in Canada for PR

skyappzbirundha said...

Thank you for this well-researched and informative article. I found the insights particularly useful and appreciated the practical examples included throughout the post. The information provided here can benefit many readers looking to improve their understanding of the topic. Great work and keep sharing. top 10 university in world.

«Oldest ‹Older   201 – 213 of 213   Newer› Newest»

Post a Comment

Cron Design Studio: Dublin based web design & software development