Skip to main content

Simple Buttons Example Script

// Declare an Enum to have type-safety for Zone IDs
// This is optional, you can also use strings (e.g. "BUTTONS") instead of Enum values (e.g. ZoneID.BUTTONS) below
enum ZoneID {
BUTTONS = "BUTTONS",
}

// Provide the Zones. They will be available in the "Control Devices" Tab for Zoning.
// Each Zone needs an id (string), a color (string) and a name (string)
ScriptBridge.provideZones([
{
id: ZoneID.BUTTONS,
color: "#00FF00",
name: "Buttons",
},
]);

// Procedurally create 20 Switch Parameters (on / off)
const buttonParameters: SwitchParameter[] = [];
for (let i = 0; i < 20; i++) {
// The SwitchParamter Constructor takes at least two arguments: id (string) and initial value (boolean)
// Make sure that the ID is unique!
// The third argument is optional, but you'll use it often. This is the callback called when the value of the
// SwitchParameter is updated. Here you can send data to an external target, for example.
const p = new SwitchParameter(`button-${i}`, false, (e: ParameterValueChangeEvent<number>) => {
// Set the color of this SwitchParameter depending on its value
// Everything except the value is held in the parameter metadata. Examples: color, label, context, ...
// Standardized Metadata Keys are available in the KnownParameterMetadata enum,
// but you can also save arbitrary data there by using any other string as the key.

// When setting a new Value or new Metadata, you always need to provide an EventOrigin.
// In the callback, you can access it using e.origin. This is useful to detect and break loops for example.
p.setMetadata(KnownParameterMetadata.COLOR, e.value ? "#00ff00" : "#030300", e.origin);
console.log(e.value ? "On" : "Off")
});
// Set some initial Metadata
p.setMetadataSeveral({
[KnownParameterMetadata.LABEL]: `Button ${i + 1}`,
[KnownParameterMetadata.COLOR]: "#000000",
}, EventOrigin.INITIALIZATION_DEFAULT);
// Add this SwitchParameter to the array of all SwitchParameters
buttonParameters.push(p);
}

// Let Glue know what Parameters belong to what Zone
ScriptBridge.provideParametersForZone(ZoneID.BUTTONS, buttonParameters);

// Set the "Plugin Status" (on the left) to "OK" (green)
ScriptBridge.ready();

More examples can be found in Glue's Script Plugin by clicking on the "Examples" button.