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).

From version 2023.1, 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. Create a JSON file that contains an array of files you want to automatically make available to all your scripting tasks.

    Here’s what the file might look like:

    Copy
    [
      "C:/WF/JSIncludes/logger.js",
      "C:/WF/JSIncludes/manifest.js",
      "C:/WF/JSIncludes/textfile.js"
    ]

    List the full path name for each of your modules.

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

  1. Save that JSON file as 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.