Reusing your code with JavaScript include files

If you use the Run Script task a lot, chances are you have written your own methods and objects, and in order to reuse them, you copy/paste them into every scripting task.

For example, many users create a wrapper around the Watch.Log() method to simplify its use. They use a variation of the following code:

Copy
function Logger() {
  this.debug = function(msg) { log(msg, 4) }
  this.info  = function(msg) { log(msg, 3) }
  this.warn  = function(msg) { log(msg, 2) }
  this.error = function(msg) { log(msg, 1) }
  this.crash = function(msg) { throw new Error(msg) }
  function log(msg, level) { Watch.Log(msg, level)}
}
var logger = new Logger();

This code allows scripts to use logger.warn("...") or logger.debug("..."), etc. which is more explicit than Watch.Log(...,2) or Watch.Log(...,4).

Instead of copy-pasting this code into every scripting task, you can add your methods and objects to Workflow's scripting engine by including JavaScript files.

Note: This feature only works with the Enhanced JScript language.

Your code must adhere to the same coding restrictions as any other Enhanced JScript task. Only reuse code that has been proven to work in Workflow

Here's how to do that.

  1. Save your code in JavaScript (.js) files.

    In the case of the example above, the following code could be saved to a file named logger.js.

    Copy
    (function() {
      function Logger() {
        this.debug = function(msg) { log(msg, 4) }
        this.info  = function(msg) { log(msg, 3) }
        this.warn  = function(msg) { log(msg, 2) }
        this.error = function(msg) { log(msg, 1) }
        this.crash = function(msg) { throw new Error(msg) }
        function log(msg, level) { Watch.Log(msg, level)}
      }
      global.logger = new Logger();
    }());
  2. Open Workflow. Click the Workflow button (W), then click the Preferences button, and under Plug-in, select Scripts.

  3. Click the Add file button, select your JavaScript file and click Open to add it. Repeat this for any other JavaScript files that need to be included.

  4. (Optional.) The paths can contain dynamic values (e.g. %{global.MyWorkFolder}/logger.js) so that the modules can be moved easily.

    To insert a variable in a path, double-click the path in the list of files to be included, type the variable name, and press Enter.

    An error message will be displayed (see Known Issues). However, the path is correctly saved including the variable name.

  5. Included files are processed in the order in which they are listed. This is important if any of the files depends on other files in the list.

    If necessary, use the Move up in list and Move down in list buttons to change the order of the files in the list.

  6. Click OK.

    The paths are saved to:
    C:\ProgramData\Objectif Lune\PlanetPress Workflow 8\PlanetPress Watch\includes.json

Sticking with the example, you could now directly use logger.warn("..."), logger.debug("..."), etc. in any Run Script task.