Run a script from another script

The p9.runScript function lets a script run another script and use its returned result. This allows you to reuse logic that is already implemented in a different script instead of duplicating it.

The target script reads the data passed to it by destructuring payload, and returns its output by assigning it to the result variable (or result.data).

This is similar to adding a global script to another script. The difference is that p9.runScript invokes a separate, independent script (identified by its id, or by its name and project) and exchanges data with it through payload/result, rather than merging in shared functions from a global script.

Procedure

  1. Right-click a code line in the Script editing pane and select Code Snippets.

    Result: The Code Snippets dialog opens.

  2. Browse the Functions → p9 category and select runScript.

  3. Select Copy.

    Result: The p9.runScript snippet is inserted directly into the Script editing pane.

  4. Identify the script to run, either by its name and project (script group name), or by its id, and pass the data the target script needs as the second argument:

    const res = await p9.runScript({ project: "test", name: "addition" }, { a: 1, b: 2 });
    console.log(res); // 3
    
    // const res = await p9.runScript({ id: "uuid of script" }, { a: 1, b: 2 });
    The target script reads its inputs from payload, and exports its output by assigning to result, for example:
    const { a, b } = payload;
    result = Number(a) + Number(b);