You'll get your Mac news here from now on...

Help TMO Grow

Advertising Info

The Mac Observer Express Daily Newsletter

More Info

Site Navigation

Columns & Editorials

by Stephen Swift

Displaying Dialog Boxes
September 17th, 2001

[Author's Note: If you find any of this tutorial not making sense, check out the some of the earlier installments.]

You run into dialog boxes everywhere when working on your Macintosh. You can create them in AppleScript as well. They may not be the neatest things you can do with AppleScript's functionality, but the end user likes them. Dialog boxes are important because they inform the user of things that happened and they can send input to the script. It also lets us practice using "if" handlers.


The basic if handler has three parts - comparison, action, and secondary action.

if someVariable is true then
--do something
--do something if it is false
end if

The if handler is used to process actions that the user has taken, such as pressing a certain button.

Basic Dialog Box

The basic dialog box is made up of four things: text, icon, buttons, and text input field (See figure 1).

Fg. 1 - The parts of a dialog box.

display dialog "the text" (*See #1*) ¬
with icon note | stop | caution (*See #2*) ¬
default answer "type something" (*See #3*) ¬
buttons ["One", "Two", "Three"] (*See #4*) ¬
default button 1 (*See #5*)

Note: I've separated this command with ¬ characters to make it easier to see the different parts of the script. Normally, this would be on one line. Here's how each of the 5 lines above breaks down:

  1. This is the only required line. Display dialog is an AppleScript command to display the text (called a string) following it in a dialog box. The command is found in the scripting addition, Display Dialog. It is found in the Scripting Additions folder of the System folder. I'll get into scripting additions later on. Think of them as extensions for AppleScript. If you drag one of them onto the script editor, you will get the dictionary for that command.
  2. If you include this line in your script, an icon will be displayed with the text. As before, I've split your three options with the | character. You can also include custom icons in your dialog boxes. See ÎCustom Icons' later on in this article.
  3. The third line adds a text field to your dialog box. The user can type something to give the script information. The string following default answer is the text displayed in the text field when the dialog is displayed. If you don't put anything in the quotation marks, the field is blank.
  4. You can have up to three custom buttons. A list always follows this object. The items (normally strings) in the list are the buttons. If you don't specify your buttons, OK and Cancel are used. It is recommended to replace the Cancel button with something else so you can control its actions. If Cancel is pressed, the script stops. If you want the user to be able to stop your script, this is good. If you don't, you should create a different button instead.
  5. The last command controls what button is selected when you hit return. You can refer buttons by their placement in the button list. Item 1 is button One, so the default button is One.

Custom Icons

You can add your own icons to your dialog boxes using ResEdit. Find an icon you want to use. Select it, choose Get Info, and copy the icon. After you save your script as an application or compiled script, drag it onto ResEdit. Press cmnd-K to create a new resource. Type 'cicn' to create a color icon resource, and hit OK. Paste the icon in the drawing box. Close the drawing box. Press cmnd-I to edit its info, type 129 (or an easy to remember number above 128), and quit out of ResEdit. What you have done is given the script an icon to use, and assigned an ID to it so you can include it in the script. When you use an ID for an icon in your script, AppleScript looks in the script file, the current application, and the System file in that order.


AppleScript records the users response so you can use it in your script. To see an example of this open Script Editor, open the result window by pressing cmnd-L, paste the following code in the main window, and hit the run button.

display dialog "Hit the OK button then look at the result." buttons ["OK", "No"]

You should see {button returned:"OK"} appear in the window. This is called a record. It is similar to a list, but each item is labeled with an object. AppleScript calls this the button returned of result. Using an if handler, we can determine what button was pressed, and what the scripts response should be. If the user hit the OK button, I want AppleScript to display a dialog box with "Thank you for hitting the OK button." If the user hit the No button, I want AppleScript to display a dialog box with "I asked you to hit the OK button. You hit the No button." This is how we would set it up: display dialog "Hit the OK button then look at the result." buttons ["OK", "No"] (*Our original dialog box.*)
if button returned of result is "OK" then (*This will return either true or false.*)
display dialog "Thank you for hitting the OK button." (*Action completed if the statement is true.*)
display dialog "I asked you to hit the OK button. You hit the No button." (*Action completed if the statement is false.*)
end if (*Closes the if statement.*)

What if there were three buttons? If handlers can only handle true or false questions. Since there would be more than two choices, we'll have to use process of elimination to narrow it down to only two choices. This is how it would be done:

if someVariable is true then
--do something
else if anotherVariable is true then
--do a second task
--do another task
end if

We have three options to test. We can only test two at a time. The first if statement tests option one and two. If option one is true, the if statement completes the first action and ends. Unlike before, we can't assume option two is true. We have to test option two. The second if statement does this. If option two is true, the if statement completes the second action and ends. Now that option three is the only one left, we can assume it is true. However, if there were four options we would have to test option three like we tested option two.


display dialog "What is your favorite flavor of ice cream?" buttons ["Chocolate", "Vanilla", "Neither"]
if button returned of result is "Chocolate" then
display dialog "I prefer Vanilla."
else if button returned of result is "Vanilla" then
display dialog "Same here."
display dialog "This script is too simple to find out what your preference is."
end if

When the user types text, it is recorded as the text returned of result. We can ask it if it is something, but that is unpractical. If the user was to type in their name, and AppleScript was to look for the word "Bob" the script might never work. What if Bob typed in Îbob' or ÎBob.'? However, this can be used to see if it is not something with the use of the ¬ si sign (option-=). If I asked for your name, and you didn't type anything, AppleScript could recognize the error. We could then use the text returned in another dialog box.

Example Script:

display dialog "What is your name?" default answer "" buttons ["OK"] default button "OK" (*Notice that I chose not to put anything in the text field.*)
if text returned of result "" then (*This makes sure the user didn't just hit OK. You could use 'is not' instead of –*)
display dialog "Hello " & text returned of result (*See the detailed explanation about this dialog box.*)
display dialog "You didn't enter a name." (*The dialog box displayed if nothing was entered in the first dialog box.*)
end if

Note: When you type out a dialog message you can include anything in it that can be converted to a string (plain text). In this case, I used the name the user entered. AppleScript takes Îtext returned of result' and converts it to the name I entered in the text field, "Stephen". You can combine different types of data with the & sign. It then converts "Hello " & "Stephen" to "Hello Stephen". I had to make sure there was a space after Hello or the dialog box would have said "HelloStephen".

Make Your Own

Now that I've covered the basic dialog box, we'll use it to make an application. Let's create an application that will ask us for a URL to go to, and it will connect us to that web page with our default browser. See Figure 2 to get an idea on what I want to create.

Fg. 2 - My application design.

We will need to make a dialog box displaying the string "Type in a URL." This can be done with display dialog "Type in a URL.". We will need to add a custom icon. I've found an icon, followed the instructions on making a custom icon, and my code uses icon 129. We will also have to include a text field with "" as the default URL. The code default answer makes the text field, and "" makes The Mac Observer the default URL. To create the two buttons we will type buttons ["Cancel", "Connect"]. To make ÎConnect' the default button we'll add default button 2 to the script.

Now we need to process the information given to us. First, we'll make sure that the user typed something by using an if statement. We need to make sure that the text returned of the result is not "" so we will type if text returned of result & "" then. Now we want to open the URL given to us. The command 'open url' does just this. If you want more information about this command, open the scripting addition "Internet Scripting" in Script Editor. The URL is the text returned from the result. So by typing open url (text returned of result) we can have AppleScript open the URL in our default browser. We've completed the first action. Type else and then we'll work on the second action. We'll inform the user that he or she didn't type in a URL. A standard dialog box such as display dialog "You didn't enter a URL." will work. We'll limit the only button to OK and make it the default button by typing buttons ["OK"] default button 1. Close up the if handler with end if, save it as an application (selecting never show startup screen), and you're done.

The finished script should look like this:

display dialog "Type in a URL." with icon 129 default answer "" buttons ["Cancel", "Connect"] default button 2
if text returned of result "" then
open URL (text returned of result)
display dialog "You didn't enter a URL." buttons ["OK"] default button 1
end if

Bonus Points...

If you thought this did the same as the 'Connect To...' application, you're right! The Connect To application is just an AppleScript similar to what we have created. Doesn't that make you feel good to know that you have created an application worthy of being on every Macintosh?


Feeling a little adventurous? There is a scripting addition called Dialog Director that allows you to create different types of dialog boxes. To use it, drop it onto your closed System folder to use it. The problem with Dialog Director is it can be difficult to use. Some genius decided to make a graphic interface for it. It makes it much easier to create cool dialogs now. It's called Dialog Studio.

What We Covered:

  1. Reviewed the if handler.
  2. Different Types Of Dialog Boxes.
  3. Using Data from the Result.

Next Time:
I'll take a break from the tutorials to write up a review of AppleScript on Mac OS X for everyone. I've played with it and so far, I see that it's going to be different, but it is going to rock!

Comments or Questions? Is this column going to slow or fast for you? Do you want to script something, but don't know how? Do you need something explained or have a question about a script?  My E-mail address, [email protected], is open 24 hours a day, 7 days a week.

Most Recent AppleScript Columns

Break Your Script Into Pieces with Subroutines
June 26th

Repeat Yourself With AppleScript
June 12th

AppleScript Lists
June 5th

AppleScript Archives

Back to The Mac Observer For More Mac News!

Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News


[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Browse the software section for over 17,000 Macintosh applications and software titles.

Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .