Subroutines with Positional Parameters

The AppleScript subroutines with positional parameters are simple to design and use, as long as you meet certain guidelines. The keywords on or to are required in the subroutine definition, followed by the name of the subroutine, and any parameters separated by commas and contained in parentheses. You have to use empty parentheses following the subroutine name if the subroutine will not take any parameters. The subroutine’s name must comply with AppleScript’s rules for identifiers. In AppleScript, the names that you create for variables and subroutines have to begin with a letter or underscore ( _ ) character, but subsequent characters can include letters, numbers, and underscores. Unless you begin and end the subroutine name with a vertical bar (|), you cannot include AppleScript’s reserved words and operators such as *, &, ^, or +, or special characters such as $, @, or #.

The end keyword is required to signal the end of the subroutine definition. You can follow end with the subroutine name for the sake of readability:

On Squared(n1)...end Squared

This is not required, however; the compiler, Script Editor, does it for you. You can declare and give values to variables in AppleScript subroutines, and use the various flow-control statements such as:

if...then...end if

and:

repeat...end repeat

However, you cannot define another subroutine inside of a subroutine definition.

To call a subroutine, use the subroutine name followed by its parameters ...

Get AppleScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.