Add a global script to another script

Global scripts are reusable scripts that can be accessed in any project for the selected script. These scripts contain functions or logic that can be shared and reused by different parts of a script.

Prerequisites

  • You have created a new script that you want to use as a global script within another main script.

  • You have created a script as a main script to insert the global script into.

  • The scripts are each in edit mode. An open lock icon for the script in the Application component pane indicates that you have opened the script and can edit it. If a script is in display mode, right-click the script and select Edit.

Procedure

  1. In the Application components pane, right-click the script that you want to use as a global script and select Mark as Global.

    Result: The script becomes a global script. In the Application components pane, the global script is placed in Globals. In the Reusable components pane, the global script project is placed as an option in the Global category.

  2. To add logic to the new global script, add functions to the global script, for example, asynchronous functions:

    const square = async function (x) {
        return x**2;
    };
    
    const message = async (x) => x;
    
    complete({ square, message });
    
    // complete allows us to export the functions we have defined
  3. In the main script, drag and drop the global script from the Reusable components pane into the main script in the Application components pane.

  4. In the main script, refer to the global script and call its functions in code, for example:

    const GlobalLib = globals.myGlobals // importing and shortenening name
    
    console.log( await GlobalLib.square(5) );
    console.log( await GlobalLib.message("Hello Neptune!") );
The Global Script and Using Globals code snippets (accessed by right-clicking a code line in the Script editing pane and selecting Code Snippets) provide default call functions to fill for a script to call the particular global script and are available in the Code Snippets dialog by default.

Results

  • You have created a global script and added the global script to a main script, calling its functions within the main script for use.