applescript

AppleScript 101: More User Input and a Simulated Conversation

This is now the fourth post in our series, AppleScript 101! If you haven’t yet, you should go check out the first few posts. You’ll need the information and techniques from those posts in order to understand this post and move forward.

Here’s what you should remember:

From Getting Started With AppleScript:

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

From AppleScript 101: Variables and Other Basic Techinques:

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

From AppleScript 101: If-Then Blocks and Try Blocks:

    • If-then blocks, including if, else, and else if
    • Try blocks

Now let’s move on to the new stuff! You guys should be starting to understand AppleScript by now, so I’m not going to comment every line – just the ones that contain new information or are confusing. Of course, if you have any questions, ask me in the comments!

What Are We Going To Do?

If you look back AppleScript 101: Variables and Other Basic Techniques, you’ll see that we started writing a script to have a conversation with the user. The farthest we got was a script that prompts you for your name and then greets you using that name.

In this post, we’re going to go much farther with that idea. We’ll include a couple methods of getting user input and end up with a pretty long script that talks back and forth with the user! Let’s get started.

A Simple Script v3: Hello title yourName!

We’re going to improve on the script a little bit so that it addresses the user with a title (Mr., Ms., Mrs.) and their name. This is going to use user input via buttons, which you haven’t learned yet! Here’s the syntax for that:

display dialog "The text you want in the dialog box" buttons { "Option 1", "Option 2", "Option 3" } default button 2

The number you put after default button is, as I’m sure you can guess, which button is initially selected when the dialog box opens. You can copy and paste the code from above into the Script Editor and see for yourself.

So, how can we take the data the user inputs? Like this:


display dialog "This is an important question" buttons { "Answer 1", "Answer 2" } default button 1
if result = {button returned:"Answer 2"} then
display dialog "You chose answer 2!"
end if

So all you have to say is if the result is Answer 2, then do something! Pretty simple, isn’t it? If the rest of that short script didn’t make sense, you should go back to the older posts and brush up a bit. Now lets move on to that conversation!

As usual, I’ll put comments inline. Remember though, I’ll only be commenting new or confusing things now! Ask if you have any problems!


display dialog "What's your last name?" default answer "My last name is..."
set theName to (text returned of result)
# This is what we just learned – it's a dialog box asking if the user is male or female.
display dialog "Are you male or female?" buttons {"Male", "Female"} default button 1
# This begins the FIRST if-then block by saying "if the answer was male, do the following."
if result = {button returned:"Male"} then
# This is the result if the answer was male.
set theTitle to "Mr."
# This is the else in the FIRST if-then block. It says "if the answer was not male, do the following."
else
# This dialog is what displays if the answer was not male (i.e. it was female)
display dialog "Are you married?" buttons {"Yes", "No"} default button 1
# This begins the SECOND if-then block. It says "If the answer to the above question was yes, do the following."
if result = {button returned:"Yes"} then
# This is the result if the user inputs first that they are a woman and second that they are married.
set theTitle to "Mrs."
# This is the else in the SECOND if-then block.
else
# This is the result of the else in the SECOND if-then block.
set theTitle to "Ms."
# This ends the SECOND if-then block.
end if
# This ends the FIRST if-then block.
end if
display dialog "Hello, " & theTitle & " " & theName & "!"

Whew! We accomplished two pretty big things there. First of all, we used the dialog box with button input. More importantly though, we encountered nested if-then blocks for the first time!

What does that mean? We used an if-then block inside of another if-then block. That was when we first asked for the gender of the user, and then if they were female, we asked another question and based the outcome on that. Pretty cool, huh? If that’s confusing at all, ask away in the comments. It’s an important topic and you should really get it down now.

A Simple Script v4: A Little Bit of Randomness

Now we’re going to add a little bit more to the script we just made. We’re going to use a random number to determine how the script is feeling when the user talks to it! Here’s the syntax for setting a random number:

set theVariable to random number from 1 to 10

Of course, 1 and 10 could be any numbers that you want the random number to be between. It’s inclusive, meaning that if you choose 1 and 2, it could be either 1 or 2. Let’s integrate that into our script!


# We're going to determine how the computer is feeling at the beginning of the script. It never changes during the script, and I like to have everything defined at the top. You could certainly move this block further down.
# This is what we just learned – randomNumber is either 1 or 2.
set randomNumber to random number from 1 to 2
# Here's an if-then block. If the number is 1, the computer is great. If it's 2, not so much.
if randomNumber = 1 then
set computerFeeling to "great!"
else
set computerFeeling to "terrible."
# Here's the end of that if-then block.
end if
display dialog "What's your last name?" default answer "My last name is..."
set theName to (text returned of result)
display dialog "Are you male or female?" buttons {"Male", "Female"} default button 1
if result = {button returned:"Male"} then
set theTitle to "Mr."
else
display dialog "Are you married?" buttons {"Yes", "No"} default button 1
if result = {button returned:"Yes"} then
set theTitle to "Mrs."
else
set theTitle to "Ms."
end if
end if
# We added a bit here. The computer now asks you (by name!) how you are doing, and gives you two options.
display dialog "Hello, " & theTitle & " " & theName & "! How are you today?" buttons { "Good, and you?", "Not great. How about you?" } default button 1
# The computer then tells you how it is feeling based on the random number way up at the top of the script!
display dialog "I'm feeling " & computerFeeling & " Thanks for asking."

Hopefully that all makes sense! I’m trying to add new concepts one at a time and make then a bit easier to swallow. Our conversation script is definitely progressing, but we might leave it there and move on to something else in the next few posts.

Wrap-Up

I hope you enjoyed this AppleScript 101 post! We’re been moving forward very well and learning a lot. In the next few posts, I have some cool things planned, so stay tuned! If you want to be notified when we post new articles, you should subscribe via email or RSS on the sidebar. If you have any questions, ask in the comments and I’ll be happy to help you out!

4 Comments

  1. andrew

    05.05.2011

    Reply

    hey i tried following your instructions and made the following code. but theres an error that says “A from:result can’t go after this text returned.”
    please help me understand what im doing wrong

    display dialog “are you male or female?” buttons {“male”, “female”}
    if result = {button returned:”male”} then
    set gender to “Mr.”
    end if
    if result = {button returned:”female”} then
    set gender to “Mrs.”
    end if
    display dialog “what is your name?” default answer “my name is…” buttons {“cancel”, “enter”} default button 2
    set myname to (text returned from result)
    display dialog “hello, ” & gender & ” ” & myname & ” ” buttons {“adios”} default button 1

    • Jeff Browning

      05.07.2011

      Hey Andrew – I’m Jeff, one of the other staff members at HackMac.org, and I’m in charge of comments.

      Evan, who wrote this article, doesn’t check his comments often, and instead hangs around at our forum (http://www.hackmac.org/forum). Feel free to sign up and ask in our “Content Support” forum, and he’ll be sure to get back to you quickly; a bunch of the guys on the community love talking about this AppleScript stuff.

    • acellier

      05.27.2011

      Try this -

      set gender to “male”
      display dialog “are you male or female?” buttons {“male”, “female”}
      if button returned of result is “female” then set gender to “Mrs.”
      display dialog “what is your name?” default answer “my name is…” buttons {“cancel”, “enter”} default button 2
      set myname to (text returned of result)
      display dialog “hello, ” & gender & ” ” & myname & ” ” buttons {“adios”} default button 1

  2. jazz14456

    11.02.2011

    Reply

    set the randomNumber to random number from 1 to 2
    if randomNumber = 1 then
    set computerFeeling to “great!”
    else
    set computerFeeling to “terrible.”
    end if
    slightly revised script that includes Ms, although its mostly for humor.

    display dialog “What’s your last name?” default answer “My last name is…”
    set theName to (text returned of result)
    display dialog “Are you male or female?” buttons {“Male”, “Female”} default button 2
    if result = {button returned:”Male”} then
    set theTitle to “Mr.”
    else
    display dialog “Are you married?” buttons {“Yes”, “No”, “I really don’t remember”} default button 1
    if result = {button returned:”Yes”} then
    set theTitle to “Mrs.”
    else if result = {button returned:”No”} then
    set theTitle to Miss
    else
    set theTitle to “Ms.”
    end if
    end if
    display dialog “Hello, ” & theTitle & ” ” & theName & “! How are you today?” buttons {“Good, and you?”, “Not great. How about you?”} default button 1
    display dialog “I’m feeling ” & computerFeeling & ” Thanks for asking.”

Leave a Reply