Lesson 4 - If statements - applescript
In this lesson, we will be discussing "if statements".
If statements are the biggest part of programming. They are used to compare variables and values.
Enter the following code into AppleScript and run it:
If statements are the biggest part of programming. They are used to compare variables and values.
Enter the following code into AppleScript and run it:
set x to 1
if x is equal to 1 then
display dialog "x = 1"
else if x is equal to 2 then
display dialog "x = 2"
else
display dialog "x is not 1 nor 2"
end if
This will return "x = 1". The reason this happened is because x was 1. Since x was not 2, the "else if" statement didn't fire. The else if is just another "if" statement inside of an if statement. Since x DID equal 1 OR 2, however, the "else" statement didn't fire. If x didn't equal 1 or 2, the else statement WOULD have fired. This is the purpose of the "else" statement. You can play around with changing the value of "x" and see what you get.
Let's break this down.
The "if" line is the beginning of the if statement. This line is one comparison. The second line (of the if statement) tells what to do if that first condition is met. The third is another comparison. The fourth tells what to do if the second condition is met. The fifth is a comparison that means "if no other condition is met". The sixth line tells what to do if no other condition is met. The seventh line tells the if statement to terminate.
Now let's figure out how to compare user input.
Enter the following code into AppleScript and run it:
Let's break this down.
The "if" line is the beginning of the if statement. This line is one comparison. The second line (of the if statement) tells what to do if that first condition is met. The third is another comparison. The fourth tells what to do if the second condition is met. The fifth is a comparison that means "if no other condition is met". The sixth line tells what to do if no other condition is met. The seventh line tells the if statement to terminate.
Now let's figure out how to compare user input.
Enter the following code into AppleScript and run it:
set x1 to display dialog "Enter Number:" default answer ""
set x2 to text returned of x1 as integer
if x2 is equal to 1 then
display dialog "x = 1"
else if x2 is equal to 2 then
display dialog "x = 2"
else
display dialog "x is not 1 nor 2"
end if
You will notice that this will tell you an answer according to whatever you decide to input. This is how you get user input. Unless what you put is not a number, then this will work. If you put in a word or symbol, AppleScript will fail out at the phrase "as integer". We will discuss how to avoid errors in a later tutorial.
Now we will discuss using buttons as a form of input.
This will return "x = 1". You can play around with changing the value of "x" and see what you get.
Now let's figure out how to compare user input.
Enter the following code into AppleScript and run it:
Now we will discuss using buttons as a form of input.
This will return "x = 1". You can play around with changing the value of "x" and see what you get.
Now let's figure out how to compare user input.
Enter the following code into AppleScript and run it:
set x1 to display dialog "Enter Number:" buttons {"1", "2", "3"}
set x2 to button returned of x1 as integer
if x2 is equal to 1 then
display dialog "x = 1"
else if x2 is equal to 2 then
display dialog "x = 2"
else
display dialog "x is not 1 nor 2"
end if
This example uses the button input as its source of input. The difference between example 2 and example 3 is that example 2 uses the phrase "text returned" with "default answer" and example 3 uses "button returned" with "buttons". It is important to set a second variable to "clean up" the data set in the first variable. It is hard, but not impossible (to the best of my knowledge) to use only one variable. I choose to use two for organization.
There are a few details I want to discuss about if statements that will benefit you, but I will not use examples for each of them as that would take a long time. The "conditions" can be changed to the following: is, is equal to, is not equal to, is greater than, is greater than or equal to, is less than, and is less than or equal to. These are some standard ones that you can use, but there are many more ways of saying these because AppleScript is a more "lenient" environment to work in and will correct you automatically.
Here is just a lazy example:
There are a few details I want to discuss about if statements that will benefit you, but I will not use examples for each of them as that would take a long time. The "conditions" can be changed to the following: is, is equal to, is not equal to, is greater than, is greater than or equal to, is less than, and is less than or equal to. These are some standard ones that you can use, but there are many more ways of saying these because AppleScript is a more "lenient" environment to work in and will correct you automatically.
Here is just a lazy example:
if x is less than y
do this
else if x is "hi"
do this
else
do this
end if
That's it! One thing you may notice is that you can use variables in your conditions as well. This is just as easy as numbers. You may also notice is that you can compare to strings as well. You now know how to use if statements to compare data.
In the next lesson, we will be discussing repeat loops.
In the next lesson, we will be discussing repeat loops.