Calling A Script

Overview

You call a script, from Java or from another script, by creating a script object with the scripts’ path or file, setting variables on it, then running it.

Calling A Script From Another Script

Here is a simple example of calling one script from another.

We have two scripts, both in a test directory:

  • test/ThingOne.groovy
  • test/ThingTwo.groovy

We’ll have them both log some Dr. Suess rhymes, but ThingOne will pass some text along to ThingTwo as a variable.

To test, create both files in the workbench, and then run test/ThingOne.groovy to see the results.

test/ThingOne.groovy

/**
 * A simple example of how to call one script from another.
 * This script is test/ThingOne.groovy. It will call test/ThingTwo.groovy.
 */

// some text to use as example variables
verse1 =
"""
'i will pick up the hook.
you will see something new.
two things.  and i call them
Thing One and Thing Two.
""";

verse2 =
"""
these Things will not bite you.
they want to have fun.'
then, out of the box
came Thing Two and Thing One!
""";

credits =
"""
    -- Dr. Seuss, "The Cat In The Hat"
""";

// imports
import com.partnersoft.scripting.ScriptLib;

// Get the script object for the other script.
// Keep in mind that we have a built-in variable already
// called "script", so it's best not to use that variable.
scriptTwo = ScriptLib.createScriptFor("test/ThingTwo.groovy");

// set variables on our script
scriptTwo.setVariable("verse1", verse1);
scriptTwo.setVariable("verse2", verse2);
scriptTwo.setVariable("credits", credits);

// do our own thing here
log.info(verse1);

// now call the script
scriptTwo.run();

test/ThingTwo.groovy

/**
 * A simple example of how to call one script from another.
 * This is test/ThingTwo.groovy. It is called from test/ThingOne.groovy.
 *
 * ThingOne.groovy passes the following variables:
 *   * verse1
 *   * verse2
 *   * credits
 */

// log the second verse only
log.info(verse2);
log.info(credits);

Log

Here is the log output from running ThingOne.groovy in the Workbench. Note how the log context changes between the two scripts.

 INFO 2014-03-10 13:28:00,701 [AWT-EventQueue-0] (com.partnersoft.workbench.editors.TextFileEditor)
    Running UndoableVfsFile test/ThingOne.groovy
 INFO 2014-03-10 13:28:00,703 [AWT-EventQueue-0] (com.partnersoft.workbench.editors.TextFileEditor)
    Running as generic script.
 INFO 2014-03-10 13:28:00,727 [AWT-EventQueue-0] (Script:test/ThingOne.groovy)

'i will pick up the hook.
you will see something new.
two things.  and i call them
Thing One and Thing Two.

 INFO 2014-03-10 13:28:00,738 [AWT-EventQueue-0] (Script:test/ThingTwo.groovy)

these Things will not bite you.
they want to have fun.'
then, out of the box
came Thing Two and Thing One!

 INFO 2014-03-10 13:28:00,738 [AWT-EventQueue-0] (Script:test/ThingTwo.groovy)

    -- Dr. Seuss, "The Cat In The Hat"

 INFO 2014-03-10 13:28:00,738 [AWT-EventQueue-0] (com.partnersoft.workbench.editors.TextFileEditor)
    Done running UndoableVfsFile test/ThingOne.groovy

Table Of Contents

Previous topic

Variables, Constants, and Settings in Scripts

Next topic

Workbench

This Page