Running Other Scripts and Handlers

Scripts and handlers outside of your initial handler can be run as commands or functions, depending on the scenario. The simplest way to run another script is to use the name of that script as a command, along with any needed parameters. For example, the following command will run the tellMeMore script:

tellMeMore 1,2,3

A specific handler within a script can be run by using the script name, a dot or apostrophe-s, and the handler name, followed by any parameters:

tellMeMore's elucidation "terse",5

utilities.insertCommas @myNumber,3

For more complex situations, the following commands and functions may be useful.

run command

Behavior: Calls the scriptName script, or the handlerName handler of that script, as a command. HandlerName is usually just the simple name of a handler without quotes, but may also be given as a quoted literal, or as an expression in parentheses to construct the handler name at run time.

The run command can be used to run a specified script when a simple command as shown above won't work, such as when a full pathname is needed or the script's name contains spaces or other special characters, or when the name of the script to be run is in a variable. The following command will run the script’s initial handler:

run "/tmp/testScript.st"

Parameters may be passed:

run "common/startup" 1200,true,"salvador"

A specific handler other than the script’s initial handler may be called, using 's:

run "common/finance" 's amortize 143000,6.75,360

The run command may be used with any object, not just a script file:

run account's accrueInterest 30, 4.125

The preceding example is equivalent to:

send accrueInterest 30, 4.125 to account

Although the run command provides similar functionality to that of the send command, it offers an alternative syntax for running a script, and also gives you the ability to run the initial handler of a script, which the send command does not provide. In addition, the run command runs the script’s initial handler directly, without calling front scripts, back scripts, or helpers.

Syntax:

run scriptName {,} {parameters}

run scriptName 's handlerName {parameters}

run function

Behavior: Calls the initial handler of a script or other object explicitly as a function. This also allows calling a script whose name is generated dynamically at runtime. The run function needs to be called in a way that sends the run message to the target object, such as using dot notation.

Syntax:

object.run(parameters)

{the} run of object {with parameters}

Example:

set doubler to (script:"return 2*param(1)")

put doubler.run(12) -- 24

get ("test" & testNumber).run(value1, value2)

-- call script dynamically

do command

Behavior: The do command evaluates expression as one or more SenseTalk statements and executes those statements as though they were part of the current handler. This means that the statements contained in expression can refer to, use, and modify any local variables of the handler (unless the evaluationContext global property is set to Global or Universal, in which case undeclared variables will be treated as global or universal rather than local variables).

Using the do command to execute some code is slower than including the same statements directly in your script, because the SenseTalk compiler is called to compile the code each time before it is executed. The impact on performance is especially significant if the do command will be executed many times, such as inside a repeat loop.

Syntax:

do expression

Example:

do "put total into quarter" & currentQtrNumber

Example:

do customCleanupCode

do AppleScript command, doAppleScript function

What They Do

Executes one or more statements written in the AppleScript scripting language that are contained in a variable or generated by your script at runtime. Any final value or error will be available in the result.

Syntax:

do AppleScript expression

do expression as AppleScript

get doAppleScript(expression)

Examples

do acquireDataScript as AppleScript

put "eggPlant" into app

-- name of the app we want to leave showing

do AppleScript merge of {{

tell application "Finder"

activate

set visible of every process whose name is not "[[app]]" to false

end tell

}}

put doAppleScript(merge("get [[A]] + [[B]]")) into sumDoneTheHardWay

Tech Talk

The do AppleScript command evaluates expression as AppleScript statements and executes those statements by calling the AppleScript interpreter. Values can be passed in at any point within the AppleScript code by enclosing local SenseTalk variable names and expressions in double square brackets [[ ]] within the code and using the merge function to insert their values before running the AppleScript.

When run using the doAppleScript function, the final value of the AppleScript expression will be the value returned by the function. Any errors will be returned as an exception object in the result. When do AppleScript is run as a command, the result will contain either an exception object or the final value of the expression.

The AppleScript code is run on the local machine. To send messages to another machine using this mechanism, the other machine will need to have remote events turned on in the Sharing panel of the System Preferences, and your AppleScript code will need to send commands to that machine using a tell statement looking something like this:

tell application "AppName" of machine "eppc://userName:password@xxx.xxx.xxx.xxx"

Consult an AppleScript reference for more details.

send command

Behavior: Sends a message to another object. Message is an expression which is evaluated as text. The message sent may include parameters, either within the message string (as in the calculateLoan example) or separately (as in the accrueInterest example). If an object is not specified, the message is sent to the object me (that is, the object containing the script).

Syntax:

send message {to object}

send message parameters {to object}

Example:

send "Hello" to newObject

Example:

send "calculateLoan 10,9,5000" to "Loan"

Example:

send accrueInterest 30, 4.625 to account

target function

Behavior: Returns the long ID of the object to which the current message was sent. Useful in a handler farther down the message passing path when you need to know what the original target of the message was. As of SenseTalk version 1.1, using the syntax the target accesses the target object directly. The target() syntax calls a function which (if not overridden by another script in the message path) will return the long ID of the target object. This will function equivalently for most purposes, although it may be slightly less efficient in some cases.

Syntax:

the target

target()

Example:

if the target is not me then ...

[ ] (square bracket messaging)

Behavior: Use square brackets to send a function-type message to a specific object. The square bracket syntax can be used by itself as a statement, or like a function call as part of an expression. In both cases, however, the message it sends is a function message, so it must be handled by a to or function handler (not an on handler).

Syntax:

[ object functionMessage ]

[ object functionMessage: parameters]

Example:

[currentLoan computeInterest:10,9,5000]

Example:

answer "The result is: " & [calculator getResult]

 

This topic was last updated on December 06, 2019, at 11:57:44 AM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2020 Eggplant