Run code when a command is triggered.
The result of a Command Script is ignored. Use it to change Swaps or send commands.
- Runs when
- The command is executed
- Typical result
- Changed Swaps or sent values
DeviceJockey Guide
Use JavaScript when a command or event needs calculations, conditions, dynamic values, or several coordinated actions.
The basic idea
DeviceJockey runs JavaScript in supported commands and events. Scripts can read and change Swaps, inspect item state, calculate values, write diagnostic messages, and use functions provided for the current context.
The result of a Command Script is ignored. Use it to change Swaps or send commands.
The returned value controls the supported item property associated with the event.
sendOSC() belongs to OSC and Behringer/Midas Command Scripts. sendArtNet() belongs to Art-Net Command Scripts.
First Event Script
This example sets a slider to its middle position:
let slider = 0.5
sliderThe last expression can also be written directly:
0.5A calculation can determine the returned position:
if (DLY_MidiSync == 1) {
let time = 60 / midiBPM
let range = 3
let sliderValue = time / range
sliderValue
}Use void 0 when a Script has no applicable value for the current situation.
void 0Command Scripts
A Local Command Script can call sendCommand(). The first argument is the Local path. The second argument is the value. An optional third argument defines its type.
sendCommand("/test", "Hello")
sendCommand("/test", 10)
sendCommand("/test", 10, "float")
sendCommand("/test", 10.5, "int")
sendCommand("/test", 1, "bool")
sendCommand("/test", true, "bool")Its purpose is to produce an action, such as changing a Swap or sending one or more commands.
Interface Script Commands
An Art-Net Script Command can update 8-bit or 16-bit DMX values.
sendArtNet(128, 1)
// 16-bit value using coarse and fine channels
sendArtNet(128, 1, 2)Build an argument array and send it to an OSC address.
let arguments = [
["Example"],
[10, "int32"],
[20, "float"],
[true, "bool"]
]
sendOSC("/test", arguments)Local Events
Use getEventValue(path) in a Local Event Script. Check for undefined before reacting, because the Script can also run during a status check without having received that Local event.
let value = getEventValue("/test")
if (typeof value !== "undefined") {
// A value was received from /test
value
} else {
// Leave the item unchanged
void 0
}sendCommand() sends the value. getEventValue() reads it in a matching Event Script.
Swaps
A Swap can be read or assigned directly by using its exact name:
console.log(mySwap)
mySwap = "Hello world"The escaped form is replaced before the Script runs. It is useful when the Swap value should become part of a string or part of the Script itself.
console.log("Hello \(mySwap)")If one Swap contains another Swap expression, use getSwapValue() to retrieve the fully replaced value.
// swapA contains the text \(swapB)
// swapB contains 10
console.log(swapA) // prints \(swapB)
console.log(getSwapValue(swapA)) // prints 10Function names and built-in variables are reserved and cannot be used as Swap names.
Remote Options
The remote() function changes which command triggers the current event executes.
remote("all", true) // enable all triggers
remote("all", false) // disable all Remote Options
remote("d", true) // disable control
remote("s", true) // mark as system event
remote("1", true) // enable trigger 1
remote("20", false) // disable trigger 20Trigger numbers from 1 through 20 are supported.
Item appearance
let red = 255
let green = 0
let blue = 0
setColorRGB(red, green, blue)
// Optional text color
setColorRGB(red, green, blue, 255, 255, 255)let hue = 360
let saturation = 100
let lightness = 50
setColorHSL(hue, saturation, lightness)
// Optional text color
setColorHSL(hue, saturation, lightness, 0, 0, 100)RGB uses values from 0 to 255. HSL uses hue from 0 to 360 and saturation and lightness from 0 to 100.
OSC event values
In supported OSC and Behringer/Midas Label Events, labelValue contains the first received argument. The oscArguments array contains all received arguments. The Script result becomes the label text.
This example converts a value from 0 to 1 into a logarithmic frequency from 200 Hz to 20 kHz:
const maxF = 20000
const minF = 200
const x = Math.min(Math.max(0.0, labelValue), 1.0)
const m = maxF / minF
const frequency = minF * Math.pow(10.0, x * Math.log10(m))
formatFrequency(frequency)
function formatFrequency(frequency) {
if (frequency < 1000) {
return Math.round(frequency).toString()
}
const k = frequency / 1000
const rounded = Math.round(k * 10) / 10
return (rounded % 1 === 0
? rounded.toFixed(0)
: rounded.toFixed(1)) + "k"
}console.log(oscArguments)MIDI Clock
A Script in a MIDI Clock event can access these built-in variables:
The current bar.
The current beat.
The current tempo in beats per minute.
True when the clock starts from stop.
True when the clock stops.
True when the clock continues from pause.
console.log(midiBar)
console.log(midiBeat)
console.log(midiBPM)Item status
These built-in variables are read-only. Availability depends on the item type.
The item value, such as a slider position from 0 to 1 or a toggle state.
True when a supported item is selected or toggled on.
True when the item light is on.
True when the item is protected from control.
Logging and color conversion
Use console.log() while developing a Script:
console.log("Script started")
console.log(itemValue)
console.log(itemSelected)
console.log(itemLight)
console.log(itemProtected)Script messages written with console.log() appear in the panel logging window under the Local Interface.
Convert between RGB and HSL when a Script calculates colors:
let hsl = rgb2hsl(red, green, blue)
let hue = hsl["h"]
let saturation = hsl["s"]
let lightness = hsl["l"]
let rgb = hsl2rgb(hue, saturation, lightness)
let convertedRed = rgb["r"]
let convertedGreen = rgb["g"]
let convertedBlue = rgb["b"]Practical workflow
Read one Swap, event value, received argument, or item status variable.
Use console.log() to verify what the Script receives and calculates.
Confirm the item reaction or command output before adding more conditions.
Check for undefined and use void 0 when the item should not change.