Basics of AppleScript

Variables & Arithmetic

In school, we use to make use of variables like x and y for algebraic operations. Similarly we make use of variables in AppleScript too.

Declaring Variables

Script [4.1.1]: set x to 5

Explanation: This will create a variable with the name x and assign it a value of 5. The result tab will print 5.

Figure 4.1.1

Figure 4.1.1 Printing Integer

Script [4.1.2]:

set x to 5
set y to 10.5 --creates a floating point number

Explanation: This will create a variable with the name x and y and assign a value of 5 and 10.5 respectively. However the result tab will print 10.5 (the last value).

Figure 4.1.2

Figure 4.1.2 Printing Floating Point Number

Another important thing here is naming of variables. There is pre-defined set of rules that needs to be followed. Click here to learn more.

Just remember one thing, the variable name should not be same as a keyword used in AppleScript like say, beep, etc. It is a good practice to composite words. It is a good practice to keep the second and additional word in title case (e.g. pictureWidth, getDivisionResult, etc).

Script [4.1.3]:

set pictureWidth to 1920
set pictureHeight to 1080
set resolution to pictureWidth * pictureHeight

Explanation: This will create a variable with the name pictureWidth and pictureHeight and assign them with a value 1920 and 1080 respectively. As specified resolution = pictureWidth x pictureHeight i.e. 1920*1080 (2073600). Hence in the result tab 2073600 will be printed.

Figure 4.1.3

Figure 4.1.3 Output of Multiplication