I'm using the following code to set a Philips Hue bulb with specific hue, saturation and brightness using Applescript. It works perfectly.
set TurnOnBulbA to the quoted form of "{\"on\": true,\"hue\": 65535, \"sat\": 240,\"bri\": 90}"
I'd like to add a random color option. This seems like it should insert a random number for the hue setting, but instead, it inserts the name of the variable:
set RandomNumA to (random number from 0 to 65535)
set TurnOnBulbA to the quoted form of "{\"on\": true,\"hue\": RandomNumA, \"sat\": 240,\"bri\": 90}"
How do I insert the generated number of the variable instead of the name of the variablel?
The following probably isn't needed, but I'll include it in case it's helpful to anybody. It's the code that completes the above for turning on a bulb:
do shell script "curl --request PUT --data " & TurnOnBulbA & " http://myIPaddress/api/myhueID/lights/1/state/"
Best Answer
You're embedding
RandomNumA
inside a literal (double-quoted) string, so it's treated as part of the literal rather than as a variable reference. To get it interpreted as a variable, you have to do the same thing you do in thedo shell script
command at the end -- use&
to append a quoted literal, a variable reference, and another quoted literal. Actually, it's slightly more complicated because you have to use parentheses to make sure the whole thing getsquoted form of
applied to it: