To define a function or a subroutine (or, a "handler"), follow the model below.
on somefunctionname(param1, param2,...)
...
return somevalue
end
A return line is required in handlers that you want to use as a function: set x to somefunctionname(param1, param2).
There is no assumption on the class of the parameters, so you may have to test the validity of the arguments, or use a try block. For instance, the following function accepts numbers as well as strings.
on minimumvalue(x, y)
if (x < y) then
return x
else
return y
end if
end minimumvalue
minimumvalue(1, 1.2)
--1
minimumvalue("a", "b")
--"a"
A script can define handlers. On their side, applications define another family of commands: "verbs". Applications, and also the Scripting Additions (or "osax") - plug-ins for AppleScript - have a dictionary that you can open in the File ▸ Open a dictionary) menu. Verbs and handlers support a slightly different syntax: the arguments of verbs are introduced, not by parentheses, but by prepositions. For instance the Satimage.osax Scripting Addition provide the following command.
format real -- the number
into string -- the formatting string, using #,^,O,.,%,',(,),+,-
[underflow test boolean] -- switch to scientific format if the number is too small with respect to the formatting string (default: false)
Result : string -- the formatted number
This means that you use the command as follows (the arguments between brackets are optional).
set s to format 1.2345 into "##.##"
--"1.23"
|