How to change switch values with a OSC message?

I need the change the values of a switch with an OSC message (sent by Max)

if I set my switch values to OSC{values}
and send a message (from Max) containing an array :
/switch_2/vals uno dos tres
nothing happens.
but if I delete my switch_2 widget, and undo, it is updated with the new values (uno dos tres)
(or if I select the field values and hit enter to evaluate it’s content, it refreshes the widget and display my values correctly)

How can I send an object of pairs “label”:value in an OSC message ? (instead of an array)
what is the stringified and OSC / Max friendly version of
{
“Value 1”: 1,
“Value 2”: 2
}

?

is there a repository of example files somewhere, demonstrating simple user cases like this one ?
I couldn’t fin any example or template O-S-C files on the website, it would really help beginners like me to start working with this amazing tool :slight_smile:

The technique you're using is correct, you've just found a bug in the switch widget :). It'll be fixed in v1 alpha soon.

How can I send an object of pairs “label”:value in an OSC message ? (instead of an array)
what is the stringified and OSC / Max friendly version of
{
“Value 1”: 1,
“Value 2”: 2
}

sending '{“Value 1”: 1, “Value 2”: 2}' as a string should do. I'm not familiar with MAX but it seems you can easily convert dict objects to JSON strings (the format used to serialize object in open stage control): The Dict Object - Max 7 Documentation

is there a repository of example files somewhere, demonstrating simple user cases like this one ?
I couldn’t fin any example or template O-S-C files on the website, it would really help beginners like me to start working with this amazing tool :slight_smile:

There no such thing currently although it's on the todo/wish list for v1.

Thanks for your help

ok, looking forward the next version to be able to make dynamic switches :slight_smile:

I didn’t manage to use a default state with OSC listener :
if I set values to
OSC{values, {
“Value 1”: 1,
“Value 2”: 2
}
}

and my switch looks like this :
image

what is the correct syntax to use an object as a defaultValue ?

about sending an object of pairs “label”:value in an OSC message :
I tried sending
/switch_2/values ‘{Value1: 1, Value2: 2}’
/switch_2/values “{Value1: 1, Value2: 2}”
/switch_2/values {Value1: 1, Value2: 2}
( comma need to be escaped with a \ in Max, otherwise it is used to separate messages…)

nothing works ; values ? shows
image
and all values are displayed in one single cell
image

(with default values, ? shows
image

The Max dict stringify doc you pointed do not really helps, as it’s a method to manipulate dict inside javascript in Max ; we need to find a solution to format a standard Max message that will be sent to a udpsend object.

would you have an example of a message containing object of pairs “label”:value sent with other softwares that is interpreted correctly in O-S-C ?

I didn’t manage to use a default state with OSC listener

The OSC listener syntax doesn't let you define complex default values, but you can circumvent this by wrapping it in a JS{{}} block:

JS{{
var oscValues = OSC{values}
return oscValues || {
  "Value 1": 1,
  "Value 2": 2
}
}}

about sending an object of pairs “label”:value in an OSC message [...]

In JSON, double quotes around keys are mandatory, so you'd need to send '{"Value1": 1, "Value2": 2}'. I've actually pushed a change yesterday to make the syntax a little less strict regarding this.

Thanks for the JS{{}} block tricks, works fine :+1:

In JSON, double quotes around keys are mandatory

arg... this is really problematic in Max :confused:
double quotes can be used only to define a string containing several items separated by spaces
"Value 1"
but if I need a key without spaces
"Value1"
Max will automatically turn it into
Value1

I managed to make it it work sending it like this from Max :
/switch_2/values "{"ValueA": 1, "ValueB": 2}"
note that the whole objects needs to be sent as a symbol in Max (between double quotes)
and we need to force double quotes around keys using \

it works, ... but it's gonna be a pain in the * to create this kind of message in Max dynamically...
(probably same kind of issue in PD and other softwares..)

In the change you've just pushed, will we be allowed to use keys without double quotes ?

In the change you’ve just pushed, will we be allowed to use keys without double quotes ?

Yes. Alpha 7 is being built/uploaded at the moment.

it works, … but it’s gonna be a pain in the * to create this kind of message in Max dynamically…

It seems there is a non-js way of creating dictionaries and output them as json in max : dict.serialize Reference - Max 8 Documentation

Anyway, it's very likely to stay this way on OSC's side.

It seems there is a non-js way of creating dictionaries and output them as json in max : dict.serialize Reference - Max 8 Documentation

yep, but dict.serialize turns a dict containing

 {
	"Value1" : 1,
	"Value2" : 2
 }

into a Max message :

Value1 : 1 Value2 : 2

which doesn't really helps :confused:

Different formats of serialization may be accessed by changing attributes to this object. However, these attributes should be used carefully.

  • the mode attribute may choose between JSON and dictionary syntax

ho, wait : investigating further the Max js dict method stringify, it looks like it does exactly what we want :grinning:

sending a dict to this js in Max does the trick :

function dictionary(_dictname) 
	{
	var d = new Dict(_dictname);
	output=d.stringify();
	outlet (0,  output);
	}

it outputs something like this :

"{
	\"Value1\" : 1,
	\"Value2\" : 2
}"

which can be sent directly to O-S-C (prepending /switch_2/values )

Hurray !

1 Like

So now, in v1, we can use json5 for /EDIT ? cool!

@cyberic /EDIT already supported json5 in v0, but the osc listeners didn’t.