applescript

AppleScript 101: If-Then Blocks and Try Blocks

In the first two posts of this series, you learned the basics of AppleScript. This post will now build on those even more by teaching you about if-then blocks, try blocks, and all the fun stuff you can do with them! Here’s what you should remember.

From the first post, remember how to:

    • Tell which application should perform the action
    • Set up a tell block
    • Run your script
    • Save your script

And from the second post, remember how to:

    • Declare a variable
    • Concatenate
    • Read comments in the code
    • Take textual user input and use it

Now let’s move on to the new stuff!

What is an If-Then Block?

If-Then Blocks, just as the name implies, execute an action depending on some other event occuring. If someEvent happens, then make someOtherEvent happen. I’ll give you a couple of examples that aren’t actually AppleScript code, but hopefully you’ll get the idea.

If the user says ‘yes’ then perform the action.
If the user says ‘no’ then don’t perform the action.
If the user says ‘addition’ then add the two numbers.

A Quick Example

This script is called “Is your number greater than 5?” All it does is say ‘yes’ if the number that the user enters is greater than 5.

# As you'll remember from the last post, this line makes a dialog box that asks the user for a number.
display dialog "What's your number?" default answer "My number is..."
# This line, as you should remember, defines the variable theNumber to whatever the user entered
set theNumber to (text returned of result)
# This is the new stuff. This line says that if theNumber is greater than 5, perform the following action inside the if-then block.
if theNumber > 5 then
# This is the action to be performed: define the variable theAnswers as Yes!
set theAnswer to "Yes!"
# This line ends the if-then block. It's just like an end tell at the end of a tell block.
end if
# Now this should all be familiar. We tell finder to make a dialog with the contents of theAnswer and then end the tell block.
tell application "Finder"
display dialog theAnswer
end tell

You’ll notice that if you put in a number less than 5, it gives you an error. That’s because we haven’t defined theAnswer if theNumber is less than 5. Let’s get to that right now.

What if they say something Else?

I’m only going to comment the new part of this script, since it’s basically the same as the old one.

display dialog "What's your number?" default answer "My number is..."
set theNumber to (text returned of result)
if theNumber > 5 then
set theAnswer to "Yes!"
# This else means "If the user says anything other than the preexisting condition(s), do the following." In this case, the condition is that theNumber be greater than 5, so this else means "If theNumber is not greater than 5, do the following."
else
# This is what happens if the number is not greater than 5: the variable theAnswer is set to Nope.
set theAnswer to "Nope."
end if
tell application "Finder"
display dialog theAnswer
end tell

Remember that the else goes inside of the if-then block (which means after the if and before the end if).

How about 3 answers?

Now we’re going to make the script do just a little more. Now we want it to tell us if the number entered is less than, the same as, or greater than 5. Let’s get going.

Again, I’m only going to comment the new stuff.

display dialog "What's your number?" default answer "My number is..."
set theNumber to (text returned of result)
if theNumber > 5 then
# We just changed the output a bit. Now it's Your number is greater than 5 instead of Yes!
set theAnswer to "Your number is greater than 5."
# Here's a new command: else if. This means that "If the input does not follow the preexisting condition(s), but does follow the condition I'm about to give you, do the following." So in this case, it means "If the input is not greater than 5, but is less than 5, do the following.
else if theNumber < 5
# If the else if is satisfied, then we set the answer to Your number is less than 5.
set theAnswer to "Your number is less than 5."
# Here's the else: "If neither of the above commands are satisfied, do the following." In this case: "If the number is neither greater than or less than 5, set theAnswer to Your number is 5!
else
set theAnswer to "Your number is 5!"
end if
tell application "Finder"
display dialog theAnswer
end tell

One Last Thing

You may have noticed that if you don’t enter a number, the script gets confused and tells you that you word, letter, symbol, or whatever is greater than or less than 5. Let’s fix that by putting in a try block to make sure that the input is a number. I’ll comment the new stuff for you.

display dialog "What's your number?" default answer "My number is..."
# This sets up a try block, just like 'if' sets up an if-then block and 'tell' sets up a tell block.
try
# This line replaces our previous line that set theNumber to the input. Now we're trying to set theNumber to the user input as long as it is a number!
set theNumber to (text returned of result) as number
# This is just like the else in an if-then block. It means that if there is an error, do the following. In this case, an error is if the input isn't a number.
on error
# This line displays a dialog box if there is an error as described above.
display dialog "That's not a number..."
# This effectively ends the script after displaying the error message.
return
end try
if theNumber > 5 then
set theAnswer to "Your number is greater than 5."
else if theNumber < 5 then
set theAnswer to "Your number is less than 5"
else
set theAnswer to "Your number is 5!"
end if
tell application "Finder"
display dialog theAnswer
end tell

So now you’ve got a complete program that validates the user’s input and tells you if the input number is greater than, less than, or equal to 5! Now, this script can’t be used for much, but I’m just teaching you the concepts so that we can make more useful scripts in the future. Look out for upcoming posts!

No comments

Leave a Reply