Filed under: Features, How-tos
AppleScript: Finder commands
Now that you've mastered the tell command, it's time to introduce some of the other AppleScript commands that you may encounter.Location command
This command will allow you to open a specific location (either on your Mac or on a web server). This command is most often used with the Finder. For instance, if you wanted to open "www.tuaw.com," then you would type:
tell application "Finder" to open location "http://www.tuaw.com"
Remember, when dealing with an application always include quote marks around the application name. When you're typing a URL, be sure to include the prefix (HTTP:, AFP:, FTP:, etc.) and include quote marks around the URL. You can also tell a specific web browser to open the URL:
tell application "Safari" to open location "http://www.tuaw.com"
However, if you use the Finder version of the script, it will open your default browser.
Say Command
You can use the say command to make your Mac talk to you. This is one of the most simple of the AppleScript commands. For instance, if I wanted the default Mac voice to say "Hello, my name is Macintosh," then I would type:
tell application "Finder" to say "Hello, my name is Macintosh."
With this command, your Mac can say anything you type; just remember to place the text in quote marks. If you want to use a voice other than your system's default add "using" after it, and then the voice name.
tell application "Finder" to say "Hello, my name is Macintosh." using "Bruce"
or
tell application "Finder" to say "Hello, my name is Macintosh." using "Vicki"
Display Dialog
This command is especially useful for shared Macs as you can use it to alert users upon login (I will get in more detail about this next time). Using this command will produce a dialog box on your screen. For instance, if I wanted to display "Hello, there," I would type:
tell application "Finder" to display dialog "Hello, there."
A small Finder dialog box will appear with the text you've just typed.
System Events
You can also use AppleScript for system events such as: shut down, sleep, restart, etc. This is how you format these commands:
tell application "Finder" to shut down
tell application "Finder" to sleep
tell application "Finder" to restart
Building a Script
Now, let's build a script linking together the Display Dialog and Say commands.
tell application "Finder" to display dialog "Hello there, click OK and I will talk to you."
tell application "Finder" to say "Hello, my name is Macintosh; I like Apples."

You've just created an AppleScript that combines more than one command. I'll expand upon this in future posts.

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


Reader Comments (Page 1 of 1)
Jeff said 9:48AM on 12-29-2007
I'm enjoying these tutorials. Thanks, and keep them coming, please.
Reply
Brian said 10:18AM on 12-29-2007
Agreed...I've been meaning to learn more applescript for awhile (I only know the basics), so I'm looking forward to where this feature will be going.
Graham said 10:37AM on 12-29-2007
Great primer! One question I've always had (maybe you'll cover it in the future) is where is the list of Applescript commands/options/functions for each app? How would I know that Finder can be told "to shut down," and where could I find out all the things iTunes can be told to do?
Reply
Cory Bohon said 10:39AM on 12-29-2007
It is known as the "Scripting Dictionary," and yes, I will be covering this in a future post. I am just trying to get the basics "ironed out."
thethirdmoose said 10:53AM on 12-29-2007
I am really glad you are making this series, because I want to learn AS. But how do you trigger these scripts? Can you have them listen for an event?
Reply
crazypenguin said 11:17AM on 12-29-2007
me too, this was very fun, i always wanted to be able to write up apple scripts, and i currently only have one that i use, even though i didn't make it. make the next lesson soon, because i can't wait to learn...i wish school was this fun.
Reply
Benjamin said 11:24AM on 12-29-2007
Thanks for this series. AppleScript was one of the things I had been hoping to learn when I switched to Mac, and these tutorials are a great help. Keep them coming!
Reply
Daniel Schlaug said 1:38PM on 12-29-2007
Great stuff this. I've known about the Dictionary in Script Editor for quite some time what puzzles me though is if there's any resource for all the other "system wide" commands? Things like delay or shut down etc. I once heard that the last official documentation on AppleScript is from when Mac OS 8 was current. Which would suggest there are no great resources out there.
Reply
michael said 1:50PM on 12-29-2007
whats the difference between the normal "display dialog" command and telling finder to do it?
Reply
le0pard13 said 2:48PM on 12-29-2007
BTW, the 'say' and 'display dialog' commands are part of Standard Additions--not the Finder. So, one can drop tell "Finder" portion to simplify this in the editor:
say "Hello, my name is Macintosh." using "Bruce"
display dialog "Hello, there."
Reply
Gazmik Fizzwidget said 3:57PM on 12-29-2007
The whole 'tell application "Finder" to' stuff is completely unnecessary for most of these commands, as they are provided by the Standard Additions.
You can use 'open location' without a tell block to open the URL in the default handler for that URL's protocol (http in Safari, mailto in Mail, vnc in Screen Sharing, etc).
The 'say' command will work the same whether enclosed in a tell block or not.
Putting 'display dialog' in a tell block will cause the dialog to be layered as if it belonged to the target app, so it can sometimes be useful. If you're running your script from Script Editor and the Finder has become frontmost in the course of running the script, using 'display dialog' without targeting Finder will cause the dialog to "belong" to Script Editor, making its icon bounce and requiring you to switch back to it to dismiss the dialog and continue the script.
Restart, sleep, and shut down actually belong to the Finder, so the tell block is actually required there. However, if you're distributing your script to others, it might be better to use the System Events app instead -- it also implements those commands, and it is always present even for users who might have disabled the Finder.
Reply
Luigi193 said 6:08PM on 12-29-2007
Thanks a lot!!! I have wanted to learn Applescript for a while now, this is a great place to do so!!!
Reply
MWakey said 7:48PM on 12-29-2007
This is all fine and well, but I still don't get it. I can click on a bookmark in my browser and get to this site faster than opening AS and typing in tell application "Finder" to open location "http://www.tuaw.com". What use is it? Can I save this script somewhere and run it with one click at a later date without opening AS and running it? Am I missing something about AS? Give me something useful to do with it or something that saves time. Seems like a lot of watsed time to create & run a script for something I can do with a few mouse clicks a lot faster.
Reply
Jon said 1:05AM on 12-30-2007
It's all about building up your Apple Script vocabulary at a steady pace that anyone can understand. It's hard to jump right into a new programming language and make something that useful immediately.
Personally, I learned a lot from this. This lesson gave me a bit of a primer in the general syntax of the language.
Keep going Cory! These tutorials are great and very easy to understand–keep 'em coming!
Meatleg said 7:39AM on 12-30-2007
yes, you can save any script as an application, so that you just click on it and it runs the script.
for instance, i have eight email accounts online that i check between work and personal, and while some of them could be configured to work in Mail, some of them cannot be. I wrote a simple script that i cal email and i saved it as an application. when clicked, this mini-app opens all of those mail sites at once. My Safari is set to open all new windows in new tabs, so one click and I have all of my online mail accounts (yahoo, yahoo japan, gmail, hotmail, and more) ready to read.
tintin77 said 10:11AM on 12-31-2007
Where can I find a list of all the available voices for the 'say' command?
Reply
.Trashes said 10:19PM on 2-13-2008
Open System preferences and click on Speech. Somewhere in there is a popup box with a list of voices.
r88my said 11:56AM on 12-30-2007
Great tutorials for a beginner! Had my mac for nearly 2 years but never opened applescript until now or even known what it does for that matter. I like what you're doing here with starting at the basics. Thanks
Reply