Basics of AppleScript

Parametrized Handler

Script [14.3.1]:

on method1(input) -- parametrized handler
    display dialog input
end method1
method1("Hey There. I am Nayan Seth.") -- passing parameters value

Explanation: Parametrized Handlers are functions in which a variable is passed.

In the above example I have created a handler named method1 and I have also passed variable input to the handler.

In the method1 block, I have displayed a dialog with the text of variable input.

Now when I call method1, I have to pass the information of parameters inside the round brackets. When we run the script a dialog will pop up with text “Hey There. I am Nayan Seth.”

Script [14.3.2]:

on area(radius) -- parametrized handler
    set circleArea to pi * (radius ^ 2)
    display dialog "Area of Circle is " & circleArea
end area
set condition to false
repeat until condition is true
    set temp to display dialog "Enter radius of Circle" default answer ""
    set r to text returned of temp
    try
        set r to r as integer
    on error
        display dialog "Enter a valid number"
    end try
    if class of r is integer then
        set condition to true
        area(r)
    end if
end repeat

Explanation: Huge program! Don’t worry... It’s super simple.

This program calculates area of circle by taking radius as input from user.

I have defined a handler named area(radius). It computes area of circle and displays it in a dialog.

But since the handler is parametrized, it needs value for the radius. [Refer to 13.3.3] To get the value of the radius, I asked for user input. But we know that user can type a string too.

So I used repeat command so that if error occurs, user can re-enter the value of radius.

The value of radius that I get from user is stored in variable r. This value is passed to handler named area(r).

Figure 14.3.2

Figure 14.3.2-2

Figure 14.3.2 Area of Circle