DeviceJockey Guide

Scripts

Use JavaScript when a command or event needs calculations, conditions, dynamic values, or several coordinated actions.

The basic idea

Scripts add logic to commands and events.

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.

Command Script

Run code when a command is triggered.

Item action → Script → Side effects

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
Event Script

Calculate how an item should react.

Event or status check → Script → Item

The returned value controls the supported item property associated with the event.

Runs when
An event occurs or the item checks its status
Typical result
Range, label, light, toggle, or protection state
Available functions depend on the Script context.

sendOSC() belongs to OSC and Behringer/Midas Command Scripts. sendArtNet() belongs to Art-Net Command Scripts.

First Event Script

Return the value that should affect the item.

This example sets a slider to its middle position:

let slider = 0.5
slider

The last expression can also be written directly:

0.5

A calculation can determine the returned position:

if (DLY_MidiSync == 1) {
  let time = 60 / midiBPM
  let range = 3
  let sliderValue = time / range
  sliderValue
}
Return nothing when the item should remain unchanged.

Use void 0 when a Script has no applicable value for the current situation.

void 0

Command Scripts

Send Local Interface values.

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")
The return value of a Command Script is ignored.

Its purpose is to produce an action, such as changing a Swap or sending one or more commands.

Interface Script Commands

Generate Art-Net and OSC output.

Art-Net

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)

OSC and Behringer/Midas

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

Read a value sent to a Local path.

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
}
Local Command Scripts and Event Scripts work well together.

sendCommand() sends the value. getEventValue() reads it in a matching Event Script.

Swaps

Use Swap names like JavaScript variables.

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)")

Resolve a Swap stored inside another Swap

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 10
Swap names must be valid and exact.

Function names and built-in variables are reserved and cannot be used as Swap names.

Remote Options

Choose commands from inside an Event Script.

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 20

Trigger numbers from 1 through 20 are supported.

Item appearance

Set item and text colors.

RGB

let red = 255
let green = 0
let blue = 0

setColorRGB(red, green, blue)

// Optional text color
setColorRGB(red, green, blue, 255, 255, 255)

HSL

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

Format received values for a label.

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

Use transport and timing variables.

A Script in a MIDI Clock event can access these built-in variables:

midiBar

The current bar.

midiBeat

The current beat.

midiBPM

The current tempo in beats per minute.

midiStarted

True when the clock starts from stop.

midiStopped

True when the clock stops.

midiContinued

True when the clock continues from pause.

console.log(midiBar)
console.log(midiBeat)
console.log(midiBPM)

Item status

Read the current state of the item.

These built-in variables are read-only. Availability depends on the item type.

itemValue

The item value, such as a slider position from 0 to 1 or a toggle state.

itemSelected

True when a supported item is selected or toggled on.

itemLight

True when the item light is on.

itemProtected

True when the item is protected from control.

Logging and color conversion

Inspect values and convert colors.

Use console.log() while developing a Script:

console.log("Script started")
console.log(itemValue)
console.log(itemSelected)
console.log(itemLight)
console.log(itemProtected)
Select the Local Interface in the logging view.

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

Build and test Scripts in small steps.

Start with one input.

Read one Swap, event value, received argument, or item status variable.

Log intermediate values.

Use console.log() to verify what the Script receives and calculates.

Return or send one result.

Confirm the item reaction or command output before adding more conditions.

Handle missing values.

Check for undefined and use void 0 when the item should not change.