14 March 2011

Max/MSP Javascript "find object" - lazy dollar function

The second function in my "Making Max JS less crap" series, this function scans all objects in the patch and all subpatches and returns a reference to the object with the specified scripting name: It's basically a lazy man's version of my dollar function which means you dont need to give every patcher a scripting name. The downside is it (probably) runs a bit slower. Enjoy!

Please see right hand pane before copying my code>>

function _$(id){ //search through all max objects:
    tempobj=0
    tempname=id
    this.patcher.applydeepif (_$act,_$scan);
    delete tempname
    return(tempobj)
}
function _$act(obj){
    tempobj=obj
}
function _$scan(obj){
    if(obj.varname==tempname){
        return(true)
    }else{
        return(false)
    }
}

//USAGE:
_$("myButton").message('set','new text!') //find the first object anywhere in the project that has scripting name myButton and send it a message.

No comments:

Post a Comment

Cron Design Studio: Dublin based web design & software development