SOLVED: In OnValue Script is "join" not the right syntax

I would like to set new entries in the menu widget.
For this I want to connect several queries in one variable. Only that the "join" command in the script does not work. What would be the right command?

var ScaleButtonName = "{}"
for (var i = 0; i < harpScale.scale.length; i++){
    ScaleButtonName = ScaleButtonName.join(harpScale.scale[i].scaleName)
    ScaleButtonName = ScaleButtonName.join(":")
    ScaleButtonName = ScaleButtonName.join(i)
    ScaleButtonName = ScaleButtonName.join(",")
}
console.log (ScaleButtonName)

Im so stupid...

I found it

var ScaleButtonName = "{"
for (var i = 0; i < harpScale.scale.length; i++){
    ScaleButtonName = ScaleButtonName + (harpScale.scale[i].scaleName)
    ScaleButtonName = ScaleButtonName + ":"
    ScaleButtonName = ScaleButtonName + i
    ScaleButtonName = ScaleButtonName + ","
}
ScaleButtonName = ScaleButtonName + "}"
console.log (ScaleButtonName)

That'd be the JS way to do it:

var ScaleButtonName = {}
for (var i = 0; i < harpScale.scale.length; i++){
  ScaleButtonName[harpScale.scale[i].scaleName] = i
}
console.log(ScaleButtonName)

(using the object notation instead of string concatenation)

I do like to create a menu for a second Menu widget.

var harpScale = get('variable_harpScale')
var ScaleButtonName = '{'
for (var i = 0; i < harpScale.scale.length; i++){
    ScaleButtonName = ScaleButtonName + '"'
    ScaleButtonName = ScaleButtonName + (harpScale.scale[i].scaleName)
    ScaleButtonName = ScaleButtonName + '":'
    ScaleButtonName = ScaleButtonName + (i + 1)
    ScaleButtonName = ScaleButtonName + ','
}
ScaleButtonName = ScaleButtonName + '}'
console.log (ScaleButtonName)
set ("menu_2A_harp_scale.values", ScaleButtonName)

The result is

{"Major":1,"Minor":2,"More":3,}

That looks ok. But now i am struggeling with the set command..

set ("menu_2A_harp_scale.values", ScaleButtonName)

.. is not setting the values on "menu_2A_harp_scale". What I am missing?

FWIW I strongly recommend using the object notation over the string concatenation method as the latter is much more error-prone.

set() can only be used to set the value of a widget, its first argument must be a widget id. You're attempting to change a property, it cannot be done directly so you have 2 options

Using a variable widget

Create a variable widget whose value will be used to define the menu's values property using the @{variable_id} syntax. Setting the variable widget's value using set() will then affect the menu's values

Using a custom variable

Write VAR{myvalues} in the menu's values property, this will create a custom variable in the menu widget which you'll be able to set using the setVar() function from some other widget's onValue script:

var ScaleButtonName = {}
// ...
setVar('menu_id', 'myvalues', ScaleButtonName)

References:

Thanks for your help... I tried to use the custom variable
Bildschirmfoto 2023-04-09 um 16.45.53
But in the Menu it accept my variable but it has no 3 buttons.
Bildschirmfoto 2023-04-09 um 16.45.12
I send the Variable that way

setVar ("menu_2A_harp_scale","myVar", ScaleButtonName)

I tried also without brackets , other brackets but nothing helped.

I think I have found the error. When I exit the For-Next loop, I have to delete the last comma before I append the last curly bracket.

Here is the result

var harpScale = get('variable_harpScale')
var ScaleButtonName = '{'
for (var i = 0; i < harpScale.scale.length; i++){
    ScaleButtonName = ScaleButtonName + '"'
    ScaleButtonName = ScaleButtonName + (harpScale.scale[i].scaleName)
    ScaleButtonName = ScaleButtonName + '":'
    ScaleButtonName = ScaleButtonName + (i + 1)
    ScaleButtonName = ScaleButtonName + ','
}
var ScaleButtonName = ScaleButtonName.slice(0,-1)
ScaleButtonName = ScaleButtonName + '}'
setVar ("menu_2A_harp_scale","myVar",ScaleButtonName)

Hence my previous statement:

FWIW I strongly recommend using the object notation over the string concatenation method as the latter is much more error-prone.

:wink:

1 Like

As you said... your object notation code is not only better but also shorter
Thank you Jean-Emmanuel

here it is in my case:

var harpScale = get('variable_harpScale')
var ScaleButtonName = {}
for (var i = 0; i < harpScale.scale.length; i++){
  ScaleButtonName[harpScale.scale[i].scaleName] = i
}
setVar ("menu_2A_harp_scale","myVar",ScaleButtonName)