Parameters and Results
Messages are identified by a single word — the message name — but they can also include additional information in the form of parameters. Parameters are values that are passed to commands or functions. These commands or functions may be custom handlers, which can either be located within the calling script, or in another script or helper suite. For more information on declaring handlers and receiving parameters, see Handlers
Two Ways to Pass Parameters: Sequential and Named
There are two ways to pass parameters when calling a command or function: sequentially, or by name. Sequential parameters are passed in sequence, as you might expect. Named parameters are passed as a property list of key:value pairs, with each key being used to match the name of a parameter on the receiving end. The two methods of passing parameters can also be combined.
The calling command or function can be in the same or a different script from the handler being called. The call is often in a separate script, which is helpful for the modularization of tests. A test structure might include a primary script with a number of different calls in it out to other scripts containing a wide variety of handlers that execute different tasks.
If you are interested in pre-defining a default value for any parameter the receiving handler might expect, see Default Parameter Values. For information on writing handlers to receive parameters, see Handlers.
Sequential Parameters
Passing Sequential Parameters
To pass parameters sequentially, simply list the parameter values after the command name separated by commas, or enclosed in parentheses after the function name when calling a function (there are some other variations for functions when only a single parameter value is being passed — see Calling Functions).
The command put "Hello, World!" sends the string “Hello, World!” as a single parameter along with the put message. To send more than one parameter, just list them with commas in between.
Example: Passing Sequential Parameters with a Command
This example passes three parameters to the doSomethingImportant command as sequential parameters: the number 31, the text green, and the variable style:
doSomethingImportant 31, "green", style
Command parameters should not be enclosed in parentheses (unless the intent is to pass a list as a single parameter).
Example: Passing Sequential Parameters with a Function
To include parameters with a function call message, list them inside the parentheses after the function name:
put roundToNearest(salary/12, 0.01) into monthlyPayTothePenny
Example: Supplying an Empty Parameter
Two commas in a row imply an empty parameter, and a final comma at the end of a list is ignored, so the following passes three parameters ("silverBar", "", and 16):
get verifyQuantity("silverBar",,16,)
Sequential Parameter Assignment
When parameters are passed sequentially, their values are assigned (in the same order) to any parameter variables that are part of the handler declaration. If the caller passes fewer values than the number of parameter variables, those that aren’t assigned will receive their default values (or empty). If the caller passes more values, the handler can access them using the parameterList function, the paramCount function and the param function.
Examples:
For example, consider a castSpell handler declared like this:
to castSpell spellName, duration, potency -- the castSpell handler declares three parameter variables
//Do things to cast the spell:
Put duration and potency into Cauldron
Stir Cauldron -- A custom call that executes the spell
Log "Alakazam!: " & spellName -- A message confirming that the spell has been cast
end castSpell
Here, spellName, duration, and potency are the parameter variables that will receive the values of parameters that are passed when the handler is called.
If the above handler is called using the command castSpell "sleep", 12 hours, "deep", then the whole script might look like this:
castSpell "sleep", 12 hours, "deep" --This is the calling command, passing three parameters sequentially
to castSpell spellName, duration, potency --The sequentially passed parameters are received in order and inserted into their corresponding parameter variables
//Do things to cast the spell:
Put duration and potency into Cauldron
Stir Cauldron -- A custom call that executes the spell
Log "Alakazam!: " & spellName -- A message confirming that the spell has been cast
end castSpell
In this case, the value “sleep” will be assigned to the parameter variable spellName, the value 12 hours will be assigned to duration and “deep” will be assigned to the potency variable.
Suppose the same handler is called using the command castSpell "sleep":
castSpell "sleep"
to castSpell spellName, duration, potency
//Do things to cast a spell!
TypeText "Alakazam!"
end castSpell
In this case only a single parameter value is supplied. The value “sleep” is assigned to the parameter variable castSpell as before, but because there are no values given for the duration and potency variables they are initialized to their default values or (as in this case, since the handler doesn’t define any default values) to empty.
The handler could also be called with more parameter values than there are parameter variables:
castSpell "sleep", 12 hours, "deep", stardust
to castSpell spellName, duration, potency
//Do things to cast a spell!
TypeText "Alakazam!"
end castSpell
In this case the first three values are assigned to the three parameter variables as usual. The additional value, stardust, is not assigned to any variable, but is available within the handler using the param function, as param(4). The number of sequential parameter values that were passed by the caller is available using the paramCount function, which in this case would return a value of 4. And the full list of sequential parameters that were passed is available as the parameterList.
The called handler can also use an ellipsis or three periods (…) to explicitly accept any number of parameters. For more information on how this is received by the handler, see Accepting a Variable Number of Parameters.
Named Parameters
Passing Named Parameters
The second method of passing parameters is by name. To pass parameters by name, use a property list (or a sequence of key:value pairs) followed by the words by name. The handler should be set up with property variables to receive the appropriate values.
Using by name has no effect if the final parameter given is not a property list or a sequence of key:value pairs.
Examples:
All of these examples show various ways of passing the same parameters as a property list, with both commands and functions.
Different ways of passing a property list by name to a command:
orderItem aPropertyList by name
orderItem {id:a, size:b, color:c} by name
orderItem id:a, size:b, color:c by name
Different ways of passing a property list by name to a function:
put itemInformation({id:a, size:b, color:c} by name)
put itemInformation(id:a, size:b, color:c by name)
Named Parameter Assignment
Any values that are passed by name are assigned to parameter variables with the same name. In order to pass named parameters, pass a property list as the only parameter, followed by the words by name. When a property list parameter (or sequence of key:value pairs) is passed by name, each parameter variable in the called handler is assigned the value of the property in the property list with the same name as the variable.
The full set of values that were passed by name is available from within the called handler using the parametersByName function.
Example: Passing a property list by name
In this example, the spellName variable is assigned the value of the spellName property from the property list, the value of the duration property is assigned to the duration parameter variable, and so forth. If the property list doesn’t have a matching property name for one of the parameter variables, that variable begins with its assigned default value (or empty if there is no default value).
set deepSleep to {spellName:"sleep", potency:"deep"} -- This stores the property list in a variable before it is passed
castSpell deepSleep by name -- Calls the castSpell handler, passing a single property list. The 'by name' keywords cause the property list values to be assigned to parameter variables whose names match the property keys
to castSpell spellName, duration, potency --The parameters passed by name are received and their values inserted into the corresponding parameter variables
//Do things to cast the spell:
Put duration and potency into Cauldron
Stir Cauldron
Log "Alakazam!: " & spellName
end castSpell
The property list being passed by name could also be the result of an expression. For example, this command adds a duration property to those in the deepSleep property list and passes the resulting properties by name:
castSpell deepSleep adding {duration: 1 hr} by name
Of course, the property list could also be given directly on the calling line:
castSpell {spellName:"sleep", potency:"deep", duration:15 minutes} by name