AppleScript Tutorial: Using Maths in AppleScript
2 March 2009
This tutorial will show you how to do basic mathematic calculations in AppleScript. This tutorial covers the following maths functions:
Variables: Setting maths variables in AppleScript
Setting variables in AppleScript is somewhat easier than other coding languages. Example AppleScript Code:
--Setting Variables
set var_x to 5
set var_y to 6
set var_c to 5
set var_x to 5
set var_y to 6
set var_c to 5
Addition: Adding numbers in AppleScript
Adding number or addition in AppleScript is pretty straight forward and is like many other coding languages. Example AppleScript Code:
-- Addition
set result_1 to var_x + var_y + 3.1
display dialog result_1 -- should return 14.1
set result_1 to var_x + var_y + 3.1
display dialog result_1 -- should return 14.1
Subtraction: Subtracting numbers in AppleScript
Subtracting number or subtraction in AppleScript is as easy as addition. Example AppleScript Code:
-- Subtraction
set result_2 to 20 - var_x
display dialog result_2 -- should return 15
Division: Dividing numbers in AppleScript
Dividing number or division in AppleScript can be done using the “/” symbol like in many other coding languages. Example AppleScript Code:
-- Division
set result_3 to var_x / var_c
display dialog result_3 -- should return 1
Multiplication: Multiplying numbers in AppleScript
Multiplying number or multiplication in AppleScript is again like many other coding languages and uses the “*” symbol. Example AppleScript Code:
-- Multiplication
set result_4 to var_x * var_c
display dialog result_4 -- should return 25
Powers: Multiply numbers by a power in AppleScript
In AppleScript, we can use the symbol "^" (keystroke shift + 6) to square or cube a number. The example below shows how to square the variable var_y. Example AppleScript Code:
-- Powers
set result_5 to var_y ^ 2 --same as var_y * var_y
display dialog result_5 -- should return 36
set result_5 to var_y ^ 2 --same as var_y * var_y
display dialog result_5 -- should return 36
Rounding: Rounding numbers in AppleScript
Rounding numbers in Applescript is based on english commands and can be easily done as shown in the examples below: Example AppleScript Code:
-- Rounding Numbers
set result_6 to round 15.2
display dialog result_6 -- should return 15
-- Rounding Up
set result_7 to round 15.2 rounding up
display dialog result_7 -- should return 16
set result_6 to round 15.2
display dialog result_6 -- should return 15
-- Rounding Up
set result_7 to round 15.2 rounding up
display dialog result_7 -- should return 16
Brackets: Using Brackets in AppleScript
Using backets number or addition in AppleScript is pretty straight forward and is like many other coding languages. Example AppleScript Code:
-- Brackets
set result_7 to round (var_x + var_y) * (var_c ^ 2)
display dialog result_7 -- should return 275 (which is 11 * 25)
set result_7 to round (var_x + var_y) * (var_c ^ 2)
display dialog result_7 -- should return 275 (which is 11 * 25)
Square Root: How do I calculate a square root in AppleScript?
Calculating the square root in AppleScript can be a little daunting at first but with the help of the code below, you should be doing it in no time. For those mathematicians out there, the square root is simply multiplying a number to the power of 1/2. Therefore with the information already provided above, calculating the square root of any number should be easy. Example AppleScript Code:
-- Square Root
-- mutliple the number to the power of a 1/2
set square_root to 9 ^ (1 / 2) -- equals 3
set square_root to 16 ^ (1 / 2) -- equals 4
set square_root to 25 ^ (1 / 2) -- equals 5
set square_root to 36 ^ (1 / 2) -- equals 6
-- mutliple the number to the power of a 1/2
set square_root to 9 ^ (1 / 2) -- equals 3
set square_root to 16 ^ (1 / 2) -- equals 4
set square_root to 25 ^ (1 / 2) -- equals 5
set square_root to 36 ^ (1 / 2) -- equals 6
And that covers it. To download the complete AppleScript, click download AppleScript below. Download Example AppleScript
Reference Documents
To review the complete AppleScript Guide as well as other AppleScript Guides for MS Word, Excel, PowerPoint or Adobe Photoshop please review the “Reference Documents” page on this website.AppleScript Reference Documents
Best of luck with your AppleScripting adventures.
Regards,
Thompson-Solutions