List Variable Manipulation Examples
These JavaScript snippets are more specific to the List
variable type.
Get the name of the currently selected item
Set the currently selected item
Create options for list variable
This function takes a list variable reference and a JavaScript array of values to add to that list variable.
/**
@param listVariable - The name or ID of a list variable
@param values - An array of values to be inserted to the list variable
**/
async function setListValues(listVariable, values) {
for (val of values) {
const rowID = (await publisher.executeFunction(`document.variables[${listVariable}].listItems`, "Add")).id;
await publisher.setProperty(`document.variables[${listVariable}].listItems[${rowID}]`, "name", val);
await publisher.setProperty(`document.variables[${listVariable}].listItems[${rowID}]`, "value", val);
}
}
/**
@param listVariable - The name or ID of a list variable
@param values - An array of values to be inserted to the list variable
**/
function setListValues(listVariable, values) {
for (val of values) {
const rowID = editorObject.ExecuteFunction(`document.variables[${listVariable}].listItems`, "Add").id;
editorObject.SetProperty(`document.variables[${listVariable}].listItems[${rowID}]`, "name", val);
editorObject.SetProperty(`document.variables[${listVariable}].listItems[${rowID}]`, "value", val);
}
}
Get the currently selected items for a multi-select list
async function getSelectedItems(listVariable) {
let selected = [];
const listLength = await publisher.getObject(`document.variables[${listVariable}].listItems.count`);
for (let i = 0; i < listLength; i++) {
const item = await publisher.getObject(`document.variables[${listVariable}].listItems[${i}]`);
if (item.isSelected == 'true')
selected.push(item.name);
}
return selected;
}
function getSelectedItems(listVariable) {
let selected = [];
const listLength = editorObject.GetObject(`document.variables[${listVariable}].listItems.count`);
for (let i = 0; i < listLength; i++) {
const item = editorObject.GetObject(`document.variables[${listVariable}].listItems[${i}]`);
if (item.isSelected == 'true')
selected.push(item.name);
}
return selected;
}
Select list items based on array
/**
@param listVariable - The name or ID of a list variable
@param items - A list of item names to select
**/
async function selectListItems(listVariable, items) {
const listLength = await publisher.getObject(`document.variables[${listVariable}].listItems.count`);
for (let i = 0; i < listLength; i++) {
if (items.includes((await publisher.getObject(`document.variables[${listVariable}].listItems[${i}].value`))))
await publisher.setProperty(`document.variables[${listVariable}].listItems[${i}]`, "isSelected", "true");
}
}
/**
@param listVariable - The name or ID of a list variable
@param items - A list of item names to select
**/
function selectListItems(listVariable, items) {
const listLength = editorObject.GetObject(`document.variables[${listVariable}].listItems.count`);
for (let i = 0; i < listLength; i++) {
if (items.includes(editorObject.GetObject(`document.variables[${listVariable}].listItems[${i}].value`)))
editorObject.SetProperty(`document.variables[${listVariable}].listItems[${i}]`, "isSelected", "true");
}
}