Basics of AppleScript

Amalgamating Learning

Script [13.2.1]:

set temp to display dialog "Enter integer" default answer ""
set i to text returned of temp
    set i to i as integer
end try
if class of i is integer then
    repeat i times
        say "Hey this works"
    end repeat
    display dialog "Please enter integer"
end if

Explanation: This program is meant for understanding exactly how we can use if...else, repeat and try together.

The aim of the script is to ask for an integer input from user and repeat “Hey this works”, ‘n’ number of times, where n is the integer input.

I began with a dialog where I asked user to provide integer input. I copied the user input to variable i. It is possible that user may enter a String. So I used try block where I perform coercion on variable i. Remember this works on integer and not on number class.

Then I use if condition to check if variable i belongs to class integer. If true then “Hey this works” will be repeated i times.

If condition is false then a dialog will be displayed asking user to enter an integer.