Lesson 3 - Operators - Applescript
In lesson 1 and 2, we learned about variables and displaying dialogs. Now we will learn how to compare and manipulate these variables according to conditions. We will start with arithmetic.
Enter the following code into AppleScript and run it:
Enter the following code into AppleScript and run it:
display dialog "" & 6 + 3
display dialog "" & 6 - 3
display dialog "" & 6 * 3
display dialog "" & 6 / 3
display dialog "" & 6 ^ 3
You will get this as the results:
9
3
18
2.0
216
Let's break this down:
Addition: "+"
Subtraction: "-"
Multiplication: "*"
Division: "/"
Power: "^"
Simple enough, now lets try this with variables
Enter the following code into AppleScript and run it:
Addition: "+"
Subtraction: "-"
Multiplication: "*"
Division: "/"
Power: "^"
Simple enough, now lets try this with variables
Enter the following code into AppleScript and run it:
set a to 6
set b to 3
display dialog "" & a + b
display dialog "" & a - b
display dialog "" & a * b
display dialog "" & a / b
display dialog "" & a ^ b
You will notice that you get the same answer as the previous example. This is because the variables are manipulated the same as the numbers themselves.
There will be times that you make the error of trying to do math to a string, so when getting a variable for user input, make sure you specify "as integer".
In the next lesson, we will go over assigning user input to variables and comparing them with "if statements".
There will be times that you make the error of trying to do math to a string, so when getting a variable for user input, make sure you specify "as integer".
In the next lesson, we will go over assigning user input to variables and comparing them with "if statements".