Basics of AppleScript

Return Statement

What if I want a handler to compute some data and return it, rather than printing it in a dialog? It’s possible.

Script [14.4.1]:

on large(a, b) -- parametrized handler
    if a > b then
        return a -- returns a value
    else
        return b -- returns a value
    end if
end large
set largest to large(15, 10) -- largest stores return value of handler large

Explanation: I have a handler named large(a,b). Its main objective is to find whether a is greater than b or b is greater than a.

However instead of displaying a dialog, I return a value. So whenever the handler is called, it gives a value which can be stored in a variable.

In above example I created a variable named largest which stores the output of large(15,10).

Figure 14.4.1

Figure 14.4.1 Return Value From Handler

Script [14.4.2]:

on square(s) -- parametrized handler
    set perimeter to 4 * s
    set area to s ^ 2
    return {area, perimeter} -- returns a list
end square
set squareList to square(5)

Explanation: This script is similar to 14.4.1. The only difference is that, the handler returns a list. And this list is stored in a variable named squareList.

Figure 14.4.2

Figure 14.4.2 Return a List From Handler