applescript

AppleScript 101: Review and Moving On

We’ve done a lot so far in our AppleScript 101 series! This post is going to review everything we’ve learned so far and then I’ll introduce what we’ll be doing in the next few installments of this series – it’s going to be really cool! Let’s jump right in.

Starting Off

In our first post, Getting Started with AppleScript, Alex introduced you to AppleScript and told you a little bit about its origins. Then he started you right off with some basic syntax.

First you learned about tell blocks. Tell blocks are expressed like this:

tell application "applicationName"

They just mean that whatever follows will be executed within the specified application. When you want to get out of the tell block, you type this:

end tell

You want to get out of the tell block when you’re done telling a specific application to perform the specified actions. Alex also gave you a few commands that you can run, but we won’t be using the ones he specified. If you want to refresh your memory, you can go check out the post.

Just a couple more things you should remember are how to run your script and save your script. If you want to run your script, all you have to do is hit the run button at the top of the editor. If you want to save it, go to File > Save As and select Application. That way you can double-click on the icon for the script and it will run without you having to open up your AppleScript editor.

Variables and Dialog

In our second post, Variables and Other Basic Techniques, I introduced you to variables. Variables are objects that contain values and can be changed. In order to declare a variable, you must write:

set variableName to "variable value"

That means that whenever you reference variableName later in your code, it will be the same as referencing variable value. I also told you how to display a dialog box. The syntax is:

display dialog "dialog content"

If you run a script that has that in it, it will display a dialog box that says dialog content. Let’s put variables and dialog boxes together:

set yourName to "Bob"
display dialog yourName

If you’re using a variable, you don’t need to put quotes around it. If the dialog box contained text we manually typed rather than a variable, we would have had to put quotes around it

There’s one last thing you learned in that post – how to take user input via a dialog box. Here’s the syntax:

display dialog "A question?" default answer "An answer"

By putting in default answer, AppleScript knows that you want input. The user will have a chance to enter their answer and then hit ok. The next step is to collect that input:

set theInput to (text returned of result)

When you put that after a dialog box with a default answer, whatever the user enters will be stored in the variable theInput. Taking in user input becomes very useful later as we move on to more complex scripts.

More Blocks

In the third post, If-Then Blocks and Try Blocks, we taught you about a couple different types of blocks. Let’s start with the if-elseif-else-then block! That sounds a lot more complicated than an if-then block, but I’m trying to condense the material since this is just a review. If you need a more in-depth explanation, go check out the post itself. Let’s start with the syntax of our big block:

if condition onethen
command one
else if condition two
command two
else
command three
end if

If blocks need an end if just like tell blocks need an end tell! See the similarities?

That looks pretty complicated, but it’s actually fairly simple. It says: if condition one is satisfied, do command one. If condition one isn’t satisfied but condition two is, do command two. Otherwise, do command three.

See? It’s not that bad. If you want a real example that actually performs an action, go back to the post. I wrote up several examples there, progressing from an if-then block to an if-else-then block to an if-elseif-else-then block!

You learned about one other thing in that post: what a try block is. Try blocks check to make sure that the user’s input conforms to what you want it to be. Again, if you want an in-dept example, go back to the post. Here I’ll just give you the basic syntax for a try block:

try
set theVariable to (text returned of result) as type
on error
error command
return
end try

There are a few things to explain here. First, this assumes that you took the user’s input via text input. It tries to set theVariable to their input, as long as their input is type. Type could be lots of things: text, number, integer, or more. If the user’s input wasn’t type, the script moves on to the error message. This would happen if you asked for a number and the user’s input was hello. If there is an error, error command is executed, and then return ends the script. Of course, the try block is closed by end try.

Now, I’m sure that all seems rather confusing. Again, if you need a concrete example, go back to the post and hopefully it will make much more sense.

Input and Randomness

In our most recent AppleScript 101 post, More User Input and a Simulated Conversation, we talked about taking user input via buttons on dialog boxes. Here’s the syntax:

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

That makes a dialog box with two answers. If the user selects answer two, a dialog box tells them what they just did. It’s a simple script, but it shows you how buttons work. Of course, the default button 1 makes it so that the first button is the one that is initially selected. If you go the post, you can see that’s there’s a very long example with nested blocks and a lot of code. I recommend you read that one, because it does a great job reviewing nearly all the concepts we’ve learned about.

There was one last thing in that post: randomness. Here’s the syntax:

set theVariable to random number from 1 to 10

All that does is set theVariable to any number from 1 to 10, inclusive. If you go back, you can see that we used that to determine how the computer was feeling randomly. Randomness can certainly come in handy in more complicated scripts.

Wrap Up

Wow! You guys have learned a lot from just a few posts. Give yourselves a pat on the back, because that’s a ton of code to know. Now you can definitely make your own useful scripts based on what you’ve learned.

I recommend write a couple scripts on your own to practice, because the next script we’ll be making is very complex – but it’s going to be very cool!

What’s Next?

Over the next few posts, we’re going to be making… a calculator! It’s going to be able to perform all sorts of mathematical operations based on user input, and will incorporate all the techniques you’ve learned so far! Look out for the posts! If you want to be updated when they come out, you should subscribe via RSS or email in our sidebar over there.

One more thing – now that you know all this AppleScript, you probably want to put it to good use. One great way to use it is to remotely execute commands on other computers. For information on how to accomplish that, check out the ebook we wrote on getting in to another computer. To get that ebook, all you have to do is subscribe to our email list – it’s right up at the top of our sidebar. You’ll also get all sorts of great tutorials and guides on how to do all things Mac, so you should subscribe!

4 Comments

  1. nike656

    09.07.2011

    Reply

    These tutorials are great! Can’t wait for the next one!

  2. Applescripter

    10.15.2011

    Reply

    Wow, the calculator seems like a very interesting project. I’m really want to see how far Alex and Evan are gonna take the calculator.

  3. Daniel

    11.08.2011

    Reply

    Thank your for the post on apple scripts I have learned quite a bit about the basics from you guys.

  4. Andrea

    05.24.2012

    Reply

    Your guide is lively, relevant, and interesting.
    You all are excellent writers, somewhat surprisingly.
    I look forward to your future posts about the calculator.

Leave a Reply