Previous Topic: Running ScriptsNext Topic: Using the run-remote Command


Using the run-local Command

Use the run-local command to evaluate script expressions or to run script files locally. The run-local command allows passing of parameters to the script code.

To evaluate an expression, specify the expression, for example:

run-local 1+1

The expression parameter is stringified, so you can specify expressions with or without quotes.

To execute a script, specify the pathname of the script in the optional –file clause:

run-local -file scripts\primes.js

The run-local command with the –file option is the only way for a running script to invoke another script. Specifying the pathname of a JavaScript file to invoke the script only works at the interactive prompt. Pathname specification is not supported inside script files. If you want to execute scripts from scripts, consider the following example:

// Begin of script file: hello.js
function hello()
{
    ? "Hello World!"
}
// End of script file

Use the following command to invoke this script:

run-local -file hello.js

This command makes the hello() function available in the current scripting context, but it does not execute it, so nothing is visible on the console. After the run-local command returns, the calling script can invoke the newly defined function to print the expected greeting:

hello();

When invoking a script file, only code that is not contained in any surrounding scope (like a function) is executed during the run-local command runs. Changing the script to the following example, prints the greeting before the run-local command returns.

// Begin of script file: hello2.js
? "Hello World!"
// End of script file

To make the function available to the call and still execute the code, the script file can contain a call to the function itself:

// Begin of script file: hello3.js
hello();
function hello()
{
    ? "Hello World!"
}
// End of script file

Directly executable code in script files can appear anywhere outside function scopes. Placing executable code in front of the first function declaration, as in the previous example, is not necessary.

You can pass parameters in an expression or in a script being run with the run-local command using the optional –with clause. The parameters are accessible in the expression or script through the standard JavaScript arguments array:

// Begin of script file: args.js
var i, l;
l = arguments.length;
for(i=0; i < l; i++)
{
    ? arguments[i]
}
// End of script file

The script prints all the arguments that are passed to it. For example, the following command prints "1", "abc" and the current date and time.

run-local -file args.js -with 1, "abc", new Date()

More Information

run-local Command--Execute a Script on the Local System (Funclet)

Accessing the Operating System

Using the OSRedirect Class