Sending a message directly to object using JS

kmll's icon

How can I send a message directly to an inlet of another object using javascript?

do.while's icon

another way is to specify JS name for an object and access it via : this.patcher.getnamed( "your varname" ).message( "built in message" , value )

kmll's icon

Thanks for the reply but i still do not get it..
As an example I create a [sel 22] object and call it "sel1". I then want to send the message "22" to the inlet of "sel1" causing it to bang. Can I please ask you to advise how I can do this using both the messnamed() function and also this.pather.getnamed("sel1").message(....

do.while's icon

Hi . sure .


this.patcher.getnamed("sel1").message('int',22);

u need to specify what type of message u want to send . in this case its an INT . as for the shorthand it could look like this


this.patcher.getnamed("sel1").int(22);

you can list all the methods of an max.object , so u will see what type of messages its expecting (for the further exploring).

var bangMe = function(){

    var sel = this.patcher.getnamed("sel1")

    listMethods(sel)

}

function listMethods(object){
    var list = []
    list = Object.getOwnPropertyNames(object)
                .filter(function(property) {
                       return typeof object[property] == 'function';
                }
                );
    post()
    post("# METHODS #")
    for(var i = 0 ; i < list.length ; i++){
        post()
        post(list[i]+"()")
    }    
    post()
    post("# END #")        
}

###### EDIT #####

as for the list of properties

function listProps(obj){


    for(var prop in obj){
        post();
        post(prop , " : " , obj[prop])    
    }

}


do.while's icon

messnamed() actually refers to a Global objects . there is reference in JS Documentation .
also with sendnamed() you can communicate for example with [send] , [receive]

kmll's icon

First I would like to thank you so much for the help so far and for the brilliant functions listMethods and listProps. I am almost about to accomplish what I set out to do but I have one final challenge. I have an object defined like this [udpsend 127.0.0.1 57120]. If send the message below to this object using a standard message box this works fine. The message I send is e.g "/oo nname set amp 1" . However I do not find a way of successfully sending the same message using javascript. When doing a listMethods on the udpsend I get the list below. Obviously the /oo is not a valid method. I have tried with ....anything("/oo nname set amp 1") etc but I do not get it to work. Is there some way I can send this message to [udpsend...]using javascript?

js: ################METHODS########################
js: bang()
js: int()
js: float()
js: list()
js: anything()
js: port()
js: host()
js: FullPacket()
js: maxqueuesize()
js: maxpacketsize()
js: sendbox()
js: ################END########################

do.while's icon

Hi !! no worries :)

dont forget that there is a message() function around . so u can easily format it like this .


var udp = this.patcher.getnamed("yourUdpBox")

var bang = function(){


    udp.message("/oo nname set amp 1")

}

EDIT : or

udp.anything("/oo"," nname"," set"," amp 1")

or

udp.list("/oo"," nname"," set"," amp 1")

EDIt 2 : this actually works fine

    udp.anything("/oo nname set amp 1")

kmll's icon

Thank you so much again for your advise. I finally get it to work now the way I want:)

This works for me
var udp = this.patcher.getnamed("nUdpsend1");
udp.message("/oo","nname","set","amp", 1);

(I did not get it to work with anything in this particular scenario but that is not important since the example above works)

Again thanks a million!!

do.while's icon

great !

thats even better because commas lets you format the message in a dynamic way (you can "variablize" things between commas ,and im sure you will use it that way ),so definitely it will work much better for you , you are flexible now .

have a lot of fun !

EDIT :
but its strange that anything() wont let you do this .