Filed under: Features, How-tos
AppleScript: the Script Editor
Before you can build your first AppleScript you need to know some basics: what a script-able application is and how to use the Script Editor.Script-able Applications
Basically, script-able applications are applications in which the developer has included a set of "instructions" that the Script Editor understands and can use. Some apps are not script-able but most Apple apps are and, of course, Mac OS X itself is script-able.
The Script Editor
The Script Editor is a very simple application, but it allows you to do some very powerful things. The Script Editor is located in your Applications folder (Applications : AppleScript : Script Editor.app) by default. The editor has a simple interface with a prominent text entry area. It also has 5 buttons in the toolbar; for now we will only focus on the "Run" and "Stop" buttons. The run button will run the currently typed script, and the stop button will cancel a running script.

Running a Script
Let's delve right into the Script Editor by running your first AppleScript! We will use the "tell" command. This command will allow you to tell a specific application to do something. For instance, if I wanted to open iTunes, I would type:
tell application "iTunes" to activate
You can substitute "iTunes" with any other application on your Mac, however, you must retain capitalization and spacing (if any) and include the quote marks around the application name. Now try typing the script and clicking the "Run" button at the top of the window. iTunes will open. Substitute activate with quit and run the script again. iTunes will now quit.
As you can see, AppleScript commands are very "English-like" in their structure. This is one reason that AppleScript is very easy to pick up as you go along.
Congratulations, you've just made your very first AppleScript!

![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
Matthew said 10:20AM on 12-27-2007
Cory,
Since your introduction I've really been looking forward to your posts on scripting and automation. I'm a long time developer (Perl, Ruby, Python, Groovy) and Systems Admin, and have been wanting to learn AppleScript for a while. So I have two comments: The articles are too short, and too infrequent! More, more more! :-) Thanks and I look forward to learning AppleScript and Automation.
Matthew
Reply
mentalsticks said 10:21AM on 12-27-2007
I agree: I want more, more, more!!
Reply
Perceval McElhearn said 10:57AM on 12-27-2007
You could also write this:
tell app "itunes" to activate
When you compile the script, Script Editor will automatically add capitals and change "app" to "application."
Reply
Peter Payne said 12:22PM on 12-27-2007
One note, AS is changed somewhat now that Leopard is here, with Automator's ability to record actions again. Some things can NOT be done easily with AS, like manipulating system preference pane options or assigning custom keyboard shortcuts, but Automator can record actions quite nicely.
Reply
Felix Pizarro said 5:16PM on 12-27-2007
I am a newbie to Mac and sort of "too busy" and I find the "little snippets" articles great! Thanks!
Reply
Quine said 5:58PM on 12-27-2007
Add me to the chorus of "more"s
Reply
warren said 11:42PM on 12-27-2007
Question, why should I open itunes anyother way than just clicking on it? I really don't understand what this apple script is for.
Please don't laugh at me, I'm rather new to mac - but I'm loving it, and I want to know more.
Reply
Jake said 6:51PM on 12-27-2007
I personally love these articles, and prefer them to be at the length they are now, so it doesn't overwhelm me! Its great and looking forward to more articles!
Reply
David Fischer said 9:13PM on 12-27-2007
AppleScript! Looking forward to more! Any how-to book recommendations?
Reply
Bakari said 3:56PM on 12-28-2007
Erica, you're just the techie to do an entire book on AS. Btw, i just saw your iPhone book in the bookstore today. Made me wish i had an iPhone. Can't wait til you get your iPod touch book published.
Reply
nicke said 5:17AM on 12-28-2007
@warren
This is just a very simple script. In its current state it is not very useful. However this snippet might be very useful if you are trying to do something more extensive. Say that later down the line, you want to do this:
1: Activate iTunes.
2: Examine which tracks in your library lack rating.
3: Collect them to a new Playlist with the title "Unrated Tracks".
4: Start playing the first track in this playlist so you can start the rating of the tracks.
None of this is useful if you don't activate iTunes first. In addition, the script I just outlined might seem like a few mouse clicks away, but if you do this a couple of times a week it gets tedious. Double click an AS applet and all of it will be done in under a second. Maybe this particular script wouldn't of use for you, but try to think of a task you do a couple of times a week, or even maybe a couple of times a day. Do you see the beauty of automating it with an AppleScript?
@cory
I, too, thought that you would write more extensively and frequently on this topic. I hope you will in the future, for this is an interesting topic!
Cheers.
Reply
Bakari said 3:59PM on 12-28-2007
Someone ask about suggest books on AppleScripting. I've just started with a 500+ page book by Stephen Kochan seems to cover the basics pretty well. I may never learn it totally, but it's fun just seeing how its done.
Reply
Mark Scrimshire said 3:20PM on 12-30-2007
I am not an Applescript expert but I was able to create an Address Book plug-in that will let you Right-Click/Option-Click on a contact's email address and lookup the person on Spock.com, the people search engine. It is crude but seems to work quite well.
The script is available to anyone that wants it at http://www.ekive.com/spock/getscript.html
Reply