Forums > Javascript

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

Nov 29 2011 | 1:19 pm

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!

Nov 29 2011 | 4:29 pm

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.


-- 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:
        post("nremoved existing v");
    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:
-----------end JavaScript "testbed.js"-----------
Nov 29 2011 | 7:13 pm

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:

many thanks

Nov 29 2011 | 11:22 pm

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

Nov 29 2011 | 11:55 pm

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!

Nov 30 2011 | 12:18 am

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

-- Pasted Max Patch, click to expand. --

Viewing 6 posts - 1 through 6 (of 6 total)

Forums > Javascript