Request for a favour! Could someone help me with this javascript?

Nov 29, 2011 at 1:19pm

Request for a favour! Could someone help me with this javascript?

Hi.. I’m humbly asking your help here :-)

I’m programming Max/MSP and M4L for about 6 months and don’t think I can also take on Java at the moment… but there’s this little thing that I seem to need Java scripting for: changing the name of the argument in a value object.

it’s illustrated in the included patcher

– Pasted Max Patch, click to expand. –

I’d be happy to return the favour if I can help!

#60338
Nov 29, 2011 at 4:29pm

You can do this with scripting, either in regular max (see the “thispatcher” help file) or with javascript. Here’s a JS example. However, I provide this mostly for pedagogical purposes. I’m not sure why you want to do this. If you are trying to access different values of data dynamically, you should investigate coll or, better yet, dict in Max6.

Peter

http://www.lividinstruments.com

– Pasted Max Patch, click to expand. –

———-begin JavaScript “testbed.js”———-
var flexval; //important to declare the maxobject globally, otherwise the “remove” won’t work.

function scriptv(){
var args = arrayfromargs(arguments);
p = this.patcher;
//delete it if it is already there:
if(flexval){
post(“nremoved existing v”);
this.patcher.remove(flexval);
}
flexval = p.newdefault(161,102,”v”,args);
flexval.varname = “valuebox”; //give a scripting name. may be useful!
//now connect it to the object named “node”
//first, find the object in the patch named node, and put it into a format that JS can understand:
var thenode = p.getnamed(“node”);
//now make the connection from outlet 0 to inlet 0:
p.connect(thenode,0,flexval,0);
}
———–end JavaScript “testbed.js”———–

#217158
Nov 29, 2011 at 7:13pm

Thank you for the suggestions Peter.
apologies for my ignorance – do I paste the javascript of your post somewhere to make it work?

I can see what you’re doing though: you’re creating the value object, and then making connections. I see I could do that with thispatcher too, which would have my preference. would you know if it’s possible to just change the name without creating the box from scratch? I’m think along the lines of the right input of a message or route box.

The purpose of my quest is to create a kind of ‘onecopy’ : you can see more detail in this thread:

http://cycling74.com/forums/topic.php?id=36599

many thanks

#217159
Nov 29, 2011 at 11:22pm

Unfortunately you can’t dynamically set the [value] name once it has been created, Peter’s suggestions are worth a look though!

#217160
Nov 29, 2011 at 11:55pm

Peter, Luke, thank you very much for your help. I think I’ve got it – and you’re right Luke, Pete’s suggestion works very well. Here’s my ‘OneCopyControl’ patch:

– Pasted Max Patch, click to expand. –

It’s use is for the following situation: when you have different patchers (maybe different versions, maybe different names even) of which you only need 1 running. Add the OneCopyControl to your patcher, loadmess an ID into the inlet, and connect a thispatcher to the outlet. With the ‘ID’ you set a unique identifier: any patcher that opens with that same identifier will close itself because there is already a patcher running carrying out the same function.

I used it to make sure only one patcher listens to a specific udp port. Also if you have a patcher in your extras, but have the same function in a different patcher (or – when you open the ‘extras’ patcher from finder) – it will prevent from 2 copies running at the same time

Am I doing something silly? any comments welcome!

#217161
Nov 30, 2011 at 12:18am

Simplified patch, thanks to grg:
In this version the ‘ID’ is simply the argument

– Pasted Max Patch, click to expand. –
#217162

You must be logged in to reply to this topic.