Subroutines with Labeled Parameters

These subroutine types are a slightly different animal than the subroutines with positional parameters, but they have some of the same rules. The keywords on or to are required in the labeled-subroutine definition, followed by the name of the subroutine, and optionally a nonlabeled parameter called a direct parameter. If the subroutine has a direct parameter (it does not have to), then the subroutine name has to be followed by the keyword of or in:

On Square of intOne...

Subroutines with labeled parameters are unwieldy to design compared with subroutines with positional parameters, but they make more subroutine calls resembling human language possible. The two different types of AppleScript subroutine designs are also a matter of personal taste, I suppose. Example 8-5 shows a labeled subroutine definition.

Example 8-5. Labeled Subroutine Definition
on Square of intOne above intLimit given Intclass:theClass
   if (intOne > intLimit) and (theClass is integer) then
      return intOne ^ 2
   else
      return 0
   end if
end Square
Square of myint above 0 given Intclass:(class of myint)

Example 8-6 redesigns the preceding subroutine using positional parameters instead.

Example 8-6. A Redesigned Subroutine Using Positional Parameters
on Square(intOne,intLimit,theClass)
   if (intLimit > 0) and (theClass is integer) then
      return intOne ^ 2
   else
      return 0
   end if
end Square

The rules on naming the subroutine (using local and global variables) and ending the subroutine definition are the same as those that apply to naming subroutines with positional parameters (see the previous section). In the following example, the direct parameter is followed by the first labeled argument, above intLimit:

on Square of intOne above intLimit given Intclass:theClass

Along with above, you can use the following AppleScript reserved words: about, against, apart from, around, aside from, at, below, beneath, beside, between, by, for, from, instead of, into, on, onto, out of, over, since, through, thru, and under. These words have no meaning in the context of calling labeled routines except to make the subroutine call more readable. Instead of writing:

on Square intOne above intLimit given Intclass:theClass

you could substitute:

on Square of intOne apart from intLimit given Intclass:theClass

and the subroutine, as long as its other elements were legal, would run the same.

If the subroutine with labeled parameters has a direct parameter (they do not have to), then the definition must also include either one of the aforementioned labels (e.g., thru), a given labeled argument, or both of these parameter types. The given label takes a variable:value pair as a parameter, which can be used to find out whether a value is true or false:

given theBool:boolVal

In Example 8-5, given Intclass:theClass is used. You then give Intclass a value when calling the routine:

Square myint above 0 given Intclass:integer

The body of the subroutine evaluates the value of Intclass.

To call one of these subroutines, you have to include the direct parameter if one has been defined for the routine. If the routine defines any of the parameters that use AppleScript’s reserved words (such as thru or above), they come next (if there are more than one, then these parameters may be called in any order). Any given parameters must come after the other labeled arguments. You have to include all the arguments defined by the subroutine—the arguments defined in AppleScript subroutines are not optional when the routine is called. Unlike the routines with positional arguments, when you call a subroutine with labeled parameters, you do not surround the arguments with parentheses. But you have to include the labels and parameter values (or any variables containing the values) in the subroutine call.