Satimage Previous | Next
Home Documentation Smile Smile Tutorial About AppleScript Functions  
To define a function or a subroutine (or, a "handler"), follow the model below.
on somefunctionname(param1, param2,...)
    return somevalue
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
        return y
    end if
end minimumvalue
minimumvalue(1, 1.2)
minimumvalue("a", "b")
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 "##.##"
Copyright ©2008 Paris, Satimage