Skip to content

Script Examples

Reading data from File

In this example, we created a UI that reads data from a CSV file.

The script:

  • reads the file,
  • splits its content by rows,
  • stores the values in an array.

These values are then assigned to the corresponding text fields to display the data dynamically.

Locator

The CSV file

Create a file named PlayerInfo.csv on the Erizos Studio Server machine. The script below reads five columns per row, in this order: first name, age, position, height and the path of the player image.

1
2
3
4
FirstName,Age,Position,Height,Image
John,24,Striker,182,mediahub:///Players/john.png
Peter,31,Goalkeeper,191,mediahub:///Players/peter.png
Sarah,27,Midfield,170,mediahub:///Players/sarah.png

Warning

File.read passes the filename straight to the file system of the Erizos Studio Server, with no root folder prepended. A bare filename therefore resolves against the working directory of the Studio Server process, which is not a location you should rely on. Use an absolute path, as the example does below.

The User Interface

The script expects the following controls:

  • dropdown1 - a Dropdown, populated from the first column of the file
  • FirstName_TI, Age_TI, Position_TI, Height_TI - Text inputs
  • PlayerImage_Media - a Media or Media Browser control
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
const file = await File.read("C:\\ProgramData\\Erizos\\Studio Server\\Resources\\PlayerInfo.csv");
const [header, ...rows] = file.split(/\r?\n/);
const val = [];
for (const row of rows) {
  if (!row.trim())
    continue;

  dropdown1.addItem(row.split(',').at(0));
  val.push(row.split(','));
}


dropdown1.onChange = () =>{
  FirstName_TI.value = val[dropdown1.selectedIndex][0];
  Age_TI.value = val[dropdown1.selectedIndex][1];
  Position_TI.value = val[dropdown1.selectedIndex][2];
  Height_TI.value = val[dropdown1.selectedIndex][3];
  PlayerImage_Media.value = val[dropdown1.selectedIndex][4];
};

Info

A UI Builder script body may use await at the top level, as the first line does - the whole script is compiled into an async function.

Tip

selectedIndex is derived from the dropdown's current value, so two rows whose first column is identical both resolve to the first of the two. Make sure the column you feed to addItem is unique.

The File API

Call Description
File.read(filename, encoding?) Reads a file and resolves to its contents as a string. encoding defaults to utf8. Throws File '<name>' does not exists when the path cannot be found.
File.write(filename, data, encoding?) Writes data to the file.
File.list(folder) Resolves to the list of names inside a folder.

All three are executed by the Erizos Studio Server, so the paths are the server's paths, not the operator's.