DesignScripting

You can use JavaScript to perform tasks in Loftware Enterprise SP by incorporating a script into a Script data source in Label Design Label Design or into a business rule in Application Design .

Loftware Enterprise™ SP uses the Rhino JavaScript engine for Script data sources and supports both the Rhino and Nashorn JavaScript engines for use in scripts in business rules. Rhino is an open source JavaScript engine maintained by the Mozilla Foundation, and Nashorn is an open source JavaScript engine maintained by Oracle. These engines enable you to parse, compile, and run JavaScript statements.

Note: JavaScript versions 1.3 and later conform to the Standard ECMA-262 ECMAScript Language Specification. For more about this specification, see "Scripting, Java, and XML" in External Links.

Using Scripts

A business rule component or a Script data source can be used to provide data for Variable Text, Text Box, Barcode, and Variable Image fields.

Field Properties

You can change a field's characteristics such as font type, boldness, size, rotation, and visibility based upon data included in the data stream, database fields on the label, or other data sources.

Example

A script can cause the font size of a text field to change based on the number of characters supplied for the field.

Math and Logic

You can use a script to perform if-then-else, mathematical, and Boolean logic on data elements included in the data stream, database fields on the label, or other data sources.

Formula Functions

You can use fields, operations, and functions also supported by the Formula data source.

Note: While the JavaScript environment provides some functionality similar to the Formula data source, the syntax is different.

Considerations

  • Because the value of a scripted field can depend on the values of other fields in the label template, you must first resolve the values of the script's dependencies.
  • As a safeguard to protect against infinite looping, a script that has not finished running after 10 seconds is terminated.
  • While Label Design Label Design allows different units of measure, Script data sources only support field positions configured in inches.

Tip: When creating local methods and variables, avoid using the names of built-in methods or variables. If a local method or variable is created with the same name as the built-in one, then the it will be overloaded with the local setting.

Built-in Variables

A script includes contains the following standard variables.

  • FORMAT: The label template containing all the fields that can be added to a canvas in Label Design Label Design.
  • MAP: The data map, a repository for all data for use when responding to a print request.
  • LOG: A class for logging information to the Loftware Application Server log file.

Built-in Helper Methods

There are built-in methods available to users for accessing components. For more information, see Script Reference.

Accessing and Modifying Data

To specify or change the value or properties of a label template field, you must be able to access the field. The structure of a label template has several components, and each component controls certain aspects of the field. Each component has methods that can be called on to access information.

FORMAT

A label templateClosed A digital representation of a label that can be combined with variable data and used by print jobs to generate print job details. is the top-level container for areas and fields in a label or a form, and it can be referred to by using the FORMAT class. The following types of components can be contained in a label template.

  • Area: A container for fields or label templates. Examples of types of areas include layer areas (which can contain fields) and reference areas (which can contain references to label templates). The default layer area in a label template is named Body.
  • AreaFormat: A component that can be used to retrieve the values of an area's formatting and positioning properties.
  • Field: An item that is displayed in a label or a form and may be contained in an area. Examples of types fields include text, barcodes, shapes, images, and controls.
  • FieldFormat: A component that can be used to retrieve the values of a field's formatting and positioning properties. The standard field has a single format field that defines the location and other properties of the field in the label or form. A barcode field can have two fields, one for the barcode and one for the human readable text.

MAP

The data mapClosed A repository for the data for use when responding to a print request. Each data map entry is composed of a name (key) and a value. is a repository for all data for use when responding to a print request. Each data map entry is composed of a name (key) and a value. The data map is referenced and updated by all data sources and business rules. The data map is then provided to the script for additional modification.

The data in the data map can be provided to the script in either of the following ways.

  • You can add a variable to the script, and that variable is converted when the script is run. This method provides the data as read only, and the method is resolved before the script runs. This is the same method used by the Formula data source.
  • You can retrieve the data from the data map during the runtime of the script. The data map can be read and written to at runtime, so you can change, add, or remove any data map entry in the data map.

LOG

The LOG variable is used to log messages to the Loftware Enterprise SP log. By default, the Loftware Enterprise SP log is located in <LOFTWARE_HOME>\product\logs. This log is managed by using the same mechanisms that are used for all logging on the Loftware Application Server. The tag for the logger is scriptLogger.

Script Example

LOG.debug("found key for value 1");
LOG.error(" an error occurred");
LOG.info("applying value");
LOG.warn(" an unexpected value was detected.");