# Script

For complex dynamic behaviors which cannot be achieved by rules, you can attach one or more scripts to widget or display to get desired behaviors. The script can be either JavaScript or Python script. For both scripts, they accept PVs as inputs and their execution are triggered by the value change event of input trigger PVs. That means the change of PV value or timestamp will trigger the execution of script. In script, the value, timestamp or severity of the input PVs are accessible, see Access PV. The widget and display are also accessible in script, see Access Widget.

For both JavaScript and Python script, it is allowed to call Java code inside by importing corresponding packages. For example:

JavaScript Example:

    importPackage(Packages.org.eclipse.jface.dialogs);
    MessageDialog.openInformation(
    	null, "Dialog from JavaScript", "This is a dialog opened from JavaScript")

Python script Example:

    from org.eclipse.jface.dialogs import MessageDialog
    MessageDialog.openInformation(
    	None, "Dialog from Python", "This is a dialog opened from Python")

As we see, the code that is calling Java code in JavaScript and Python are very similar. So most script examples in this help document are in JavaScript, but it should be easy for you to translate them to Python. For example, here are two code snippets written in JavaScript and Python respectively. They are totally same in functionality.

JavaScript Example:

    importPackage(Packages.org.csstudio.opibuilder.scriptUtil);
    var value = PVUtil.getDouble(pvs[0]);
    var RED = ColorFontUtil.RED;
    widget.setPropertyValue("start_angle", value);
    widget.setPropertyValue("foreground_color", RED);

Python script Example:

    from org.csstudio.opibuilder.scriptUtil import PVUtil
    from org.csstudio.opibuilder.scriptUtil import ColorFontUtil
    value = PVUtil.getDouble(pvs[0])
    RED = ColorFontUtil.RED
    widget.setPropertyValue("start_angle", value)
    widget.setPropertyValue("foreground_color", RED)

### Steps to attach scripts to a widget:

1. Select Scripts property in property sheet view.
2. In Attach Scripts Dialog, you can add more than one script. For each script, you can specify more than one PV as the inputs. For PVs that will trigger the script execution, they should be marked as yes in trigger column. There must be at least one trigger PV for each script. Otherwise, the script will not execute because there is no other trigger source.
   In the Options page, there are two options for each script execution. In most cases, you do not need to change the default settings.
   - If the first option is checked, the execution triggered by PVs' first connections will be skipped. During OPI startup, the OPI will try to connect to all PVs. Once the trigger PVs are connected, they will trigger the script execution. In some cases, you may don't expect this execution. For example, you want the script only executes when you click a button which will write a value to the trigger PV of the script.
   - By default, the script will not execute if any one of the input PVs is disconnected. But if the second option is checked, the script will execute even if there are some input PVs are not connected. If there are code in the script trying to read value of the disconnected PVs, you may get an exception and the script will not be executed afterwards!

3. Save and run the OPI to see the result.

### Learn more

- JavaScript
- Python Script
- Access Widget
- Access PV
- Property Type
- Script Utility API