Introduction to VivoScript

VivoScript is a scripting language built into VivoQuant. Its primary goal is to enable task automation, making it easier to streamline repetitive or complex workflows.
With VivoScript, nearly all functionality in VivoQuant can be accessed programmatically, making it a powerful tool for advanced users.

It is based on JavaScript (ECMAScript), so anyone who knows JavaScript can read and write VivoScript.

The VivoScript Language

VivoScript runs on the JavaScript engine built into Qt, the framework VivoQuant is built with. That engine supports JavaScript up to the ECMAScript 7 (2016) standard, which includes let and const, arrow functions, classes, template literals, destructuring, Map and Set, and Promises. Features added in later standards, such as async/await or object spread ({...obj}), may not be available.

VivoScript is not a web browser or Node.js: there is no web page (document, window), no fetch, and no require or file system module. VivoQuant’s own functions take their place, reached through the VQ object:

var dm = VQ.dataManager();            // the loaded datasets
dm.setPalette(VQ.index(0), "gray");   // change the first dataset's palette
VQ.showMessage("Palette updated");    // show a message to the user
console.log("Done");                  // print a line

What a script prints with console.log(), console.info(), console.warn(), console.error() or print() is written to the log and shown in the VivoScript Console. When a snippet or script runs from the Workflow Assistant, it is also shown in its Output area. Lists are printed in brackets and objects as [object Object]; use console.log(JSON.stringify(value)) to see what an object contains. console.time(), console.count() and console.assert() write to the log only.

All objects and functions available to scripts are listed in the VQScript Classes reference below. To learn the JavaScript language itself, see the MDN JavaScript Guide or the free Codecademy course Introduction to JavaScript (lessons 1 to 6 and 8).

Including Other Scripts

A script can reuse functions from other script files with #include:

#include "commonfuncs.vqs"
#include "lib/stats.vqs"

var mean = computeMean(values);   // defined in lib/stats.vqs
  • Each #include must be on a line of its own, with the file name in double quotes. A comment may follow it on the same line, either // ... or a /* ... */ that ends on that line: #include "SettingsTab.vqs" // the Settings tab.
  • Before the script runs, VivoQuant replaces each #include line with the contents of that file, so everything the file defines can be used in the rest of the script. Code outside of functions in an included file runs at the point where the file is included.
  • Error messages still point at the original file and line, for example Line: 12 (stats.vqs).
  • #include is processed for script files that VivoQuant runs, such as Quick Scripts, for code typed into the VivoScript Console and for snippets in a workflow. It is not processed when Run VQS with import support is turned on in the VivoScript configuration.
  • To turn an include off, put // in front of it. An #include line inside a /* ... */ comment is still processed.

Where VivoQuant Looks for Included Files

An absolute path, such as #include "C:/MyScripts/common.vqs", is used exactly as written.

Any other name is looked up in the following folders, in this order. The first file found is used.

  1. The folder of the script being run. For a workflow snippet, the folder of the workflow. Code typed into the VivoScript Console has no folder, so the search starts at the next step.
  2. The folder named by the VQSCRIPT_PATH environment variable, if it is set.
  3. The current working directory, that is, the folder VivoQuant was started from.
  4. The VivoScript folder in your personal folder: Documents on Windows, your home folder on macOS and Linux.
  5. On macOS only: the VivoScript folder in the application’s Resources folder (VivoQuant.app/Contents/Resources/VivoScript).
  6. The VivoScript folder in the VivoQuant program folder, for example C:/Program Files/Perceptive/VivoQuant/VivoScript on Windows.

Folders 5 and 6 are VivoQuant’s own script folders: the scripts installed with VivoQuant, including libraries such as SettingsTab.vqs and the iPACS library, are in folder 5 on macOS and folder 6 on Windows and Linux. To leave them out, turn on Don’t #include scripts from VivoQuant’s own script folders in the VivoScript configuration. The folder of the script being run is always searched, so a script kept in one of these folders still finds the files next to it.

A name that contains folders, such as lib/stats.vqs, is looked up the same way: VivoQuant checks for lib/stats.vqs inside each of these folders in turn.

Every #include uses this same order, including an #include inside a file that was itself included. Names are never looked up relative to the file that contains the #include. For example, if run.vqs includes lib/stats.vqs and stats.vqs needs lib/math.vqs, write #include "lib/math.vqs" in stats.vqs, not #include "math.vqs".

Each File Is Included Only Once

When several scripts include the same file, its contents are inserted the first time only, and later #include lines for that file are skipped. This also stops scripts that include each other from looping. A file reached through two different paths, for example through a symbolic link, counts as the same file.

Name Conflicts

If a name matches files in more than one of the folders above, VivoQuant uses the first one. Before the script runs, it shows a single warning that lists each such name, the file that was used and the files that were ignored. Files with identical contents are listed too, because copies can drift apart later.

To resolve a conflict, rename one of the files, delete the copy you don’t need, or include the file you want by its absolute path. If you keep your own copies of scripts that are also installed with VivoQuant, turn on Don’t #include scripts from VivoQuant’s own script folders in the VivoScript configuration: your copies are then always used, without a warning.

Missing Files

If an included file can’t be found, VivoQuant shows a Script Error warning that lists the folders it searched. The script still runs, so it usually fails later, where it first uses something from the missing file.

Script Settings

Scripts that read their settings from a file can use SettingsTab.vqs, a library installed with VivoQuant. It shows the settings on a Settings tab of the script panel, where the user can check and edit them before the script runs, and it reads, checks and saves the settings file for the script.

#include "SettingsTab.vqs"

var settings = new SettingsTab({
    title:       "My tool settings",
    defaultFile: "MyToolSettings.txt",      // next to the script
    configKey:   "myToolSettings",          // remembers the last file used
    groups:   [ { id: "data", label: "Data" } ],
    settings: [
        { key: "url",  group: "data", label: "Server", type: "text", required: true, help: "iPACS server URL" },
        { key: "mode", group: "data", label: "Mode",   type: "item", items: ["Fast", "Full"] }
    ]
});
var params = settings.run();   // { url: ..., mode: ... }
  • run() starts with the settings file used last time, otherwise the default file. If that file doesn’t exist yet, the tab starts with the default values and the file is created when the user presses Run.
  • Each setting is a field on the tab, in a section for its group. Settings can be text, a choice from a list, a check box, a date (YYYYMMDD), a list of text, or a whole or decimal number.
  • Problems are shown in the status line above the Run button as the user types, in red. Problems found in the file, such as an unknown setting, are shown in orange, with their line number. Pressing Run while there are problems shows a message explaining them, and the script waits until they are fixed.
  • Use another file opens a different settings file.
  • When the user presses Run with valid settings, any changes are saved to the file, and run() returns the values. The tab then stays as a read-only record of the settings in use; to change them, run the script again.

Settings files have one key = value per line. Lines starting with // or # are comments. When a file is saved it is written in a standard form, with each setting’s help text as a comment above it.

The full list of options and the settings file format are described at the top of SettingsTab.vqs.

The VQScript Toolbar

Adding Script Shortcuts to the Toolbar

Shortcuts for commonly executed VQScripts can be added to the Quick Scripts menu on the VQScript toolbar

To add a Quick Script shortcut:

  1. Go to Tools->Configuration and click on the VivoScript tab.

    Add Quick Script

  2. Select a script for which quick access is desired by clicking the Add Quick Script button Add Quick Script and navigating to the directory where the script is saved.
  3. Add a nickname for the script in the dialog box that appears.
  4. The script will now appear in the configuration window as well as in the Quick Scripts menu on the VQScript toolbar.

Quick Scripts can be edited directly from the VQScript toolbar by clicking on the Edit VivoScript button Edit VivoScript . A window will appear in which the user can edit the script.

Edit Quick Script
Edit Quick Script

Using the Peek Tool

The Peek Tool is used to reveal the names of elements (called widgets) in the operator GUIs for use in scripts. The command VQ.getWidget("widgetName") can then be used to interact with these elements.

For example, to determine the name of the button in the 3D ROI operator that resets all ROIs, click on the Peek button Peek Tool on VivoQuant’s top bar, and then click on the desired button in the 3D ROI operator operator box. A yellow box containing the class and name of the selected button will be displayed.

Using the Peek Tool
Using the Peek Tool

Then, the command VQ.getWidget("buttonResetROIs").click() can be used to click the button and reset all ROIs.

VQScript Examples

Included with VivoQuant are VQScript example scripts, and libraries for scripts to include, to help you get started working with VQScript. These scripts illustrate some of the many ways VQScript may be used to streamline workflows in VivoQuant. To learn more about each file, see the VQScript Example Scripts page.

VQScript in Workflows

The Workflow Assistant can run VivoScript from a workflow step, either a short snippet written in the step or a link to a script file. See Running VivoScript from a Workflow.

VQScript Classes

Click here to see the list of VivoScript Classes.

Named Constants (Enumerations)

Some VQScript functions take an argument from a fixed list of choices, such as a DICOM format, a dataset slot (Reference, Input1, Input2) or a view plane. These choices are available as named constants on the VQ object, for example VQ.DICOMImageFormat.MFGroup, VQ.DataID.Input1, VQ.Plane.Transverse or VQ.CropView.DASHED.

In the class reference these arguments appear with the name of their list, for example void setActive(DataID data_id, bool visible, int viewport_id). Write such a value as VQ.DataID.Input1.

Each named constant is simply a name for an integer, so existing scripts that pass the integer values keep working unchanged. Both forms can be mixed in the same script:

var ctl = VQ.controller();
ctl.setActive(1, false);                  // integer form, still supported
ctl.setActive(VQ.DataID.Input1, false);   // same call, easier to read

New scripts should prefer the named form. See the Enumerations page of the VQScript documentation for the full list of constants and their integer values.