Looping and Variables
The syntax

Table of Contents:

Value Evaluation
AppleScript Defined Variables
Declaring Variables
Predefined Variable Types
Conditional Statements and Looping
Practical Examples

Value Evaluation

Almost every command Examplesyou have AppleScript execute generates a "result"."result" is a reserved word by AppleScript. After the script executes its last line, AppleScript will open a "the result" window showing the value of "result" if there is one to display.


Just a number on a line by itself like the above is a valid AppleScript. When it is run, AppleScript will open "the result" window with that value.

set aNum to 3929192

This also opens "the result" window with the value: 3929192. Again, this is because the right side of the expression gives off a "result" which is stored in aNum.


AppleScript Defined Variables

There are a few variables that AppleScript reserves to use at all times for your convienience:

result: «script»

result: «script»

{pi, return, space, tab}
result: {3.1415926535898, "
", " ", " "}


Declaring Variables

AppleScript is very flexible in how it lets you define variables. They do not have to be predeclared like as in C and Pascal. It's as simple as:

set myVariable to 1

Variables are type-less.

set myString to "hello"

set myNumber to 69.31337251

set aCharactor to "c"

Variables can be "coerced" into other types.... but only if there is a coercion-conversion for the two types.

set aChar to (first character of "hello")
result: "h"

set aWord to (first word of "Hello World")
result: "Hello"

set aStringNumber to "3.1457"
result: "3.1457"

set aNum to "3.1457" as number
result: 3.1457

set aInteger to "8675309" as integer
result: 8675309

Copies of variables can be made just by assignment...

set myHello to "hello"

set anotherHello to myHello

Predefined Variable Types

 Class Name Example
Boolean true
Integer 21
List {5, "Steve", 89.4}
Real 21.0
String "Steve"
Date date "Sunday, November 3, 1996 1:00:00 PM"
Record {name:"Steve", age: 21, hair:"blonde"}
Reference list "Users" of application "UnderControl"
Class string
Constant pi
Data {«data ...» ...}



A property can almost be called a constant. As soon as it is modified though, AppleScript acts differently. Properties can be stored and recalled every time a script is re-run.

An example of this is a counter script. A property can be made that is the current counter number. If each time the script is run, the counter is incremented, you can effectively keep count of how many times a script was run.

property aCounter : 0 -- this is the absolute inital value of the counter
set aCounter to aCounter + 1

Save this to a file or use the premade one.

Try running this script a couple times. Note that the resulting number keeps increasing! Now, modify the script and "Check Syntax". Now, the counter was reset.


Conditional Statements And Looping

The basic syntax for loops are all derivations of the "repeat" structure:

[statement] ...
end [repeat]

repeat numberOfTimes [times]
[statement] ...
end [repeat]

repeat until BooleanExpression
[statement] ...
end [repeat]

repeat while BooleanExpression
[statement] ...
end [repeat]

repeat with counterVariable from startValue to stopValue [by stepValue]
[statement] ...
end [repeat]

repeat with loopVariable in list
[statement] ...
end [repeat]


The basic syntax for conditional statements are:

if BooleanStatement then [statement]

if BooleanStatement then
[statement] ...
end [if]

if BooleanStatement then
[statement] ...
else if BooleanStatement then
[statement] ...
[statement] ...
end [if]


Practical Examples

Demonstration/Explanation of SpellBot.

Sample code for calculating pi.

display dialog "calculating pi, please wait..."
set a to 1
set b to 3
set c to -1
repeat 20000 times
set d to a
set a to a + c / b
set b to b + 2
set c to c * -1
end repeat
"pi =" & 2 * (a + d)

Go back to the Table Of Contents
An AppleScript Tutorial for ACM's MacWarriors group by Steven Bytnar
Copyright 1996, Steven Bytnar