How to customize AppleScript dialogs (dialog boxes buttons and icons)

AppleScript FAQ: How can I customize AppleScript dialog boxes?

When you display an AppleScript dialog box with the display dialog code, you don't have to just use the standard Cancel and OK buttons, you can change the text to something more meaningful to your specific prompt. For instance, suppose you want to ask someone their age range:

display dialog "How old are you?" buttons {"Less than 1", "Older than 1"}

(Okay, not the greatest example in the world, but that’s all I've got today.)

That code results in an AppleScript dialog that looks like this:

AppleScript dialogs: How to specify a default button

Note that this dialog doesn't have a button selected by default. A cool addition to this is that you can specify the default button when the dialog is displayed, like this:

display dialog "How old are you?" buttons {"Less than 1", "Older than 1"} default button 2

That example highlights the "Older than 1" button when the dialog is displayed. This is a nice option for when you want to specify a default AppleScript dialog button.

How to get the user input (result) back

Of course it's not too useful to prompt someone like this unless you can also tell which button they clicked. You get the AppleScript button result back like this:

-- the following command should all be on one line, but I 
-- wrapped it here readability
set answer to the button returned of 
(display dialog "How old are you?" 
buttons {"Less than 1", "Older than 1"} default button 2)

-- display the answer
display dialog "You selected " & answer

How to add icons to your AppleScript dialogs

You can also add an icon to your dialog using the AppleScript "with icon" syntax. Here are a few simple examples. First, the AppleScript note icon:

display dialog "Hello" with icon note

AppleScript dialog with note icon

Next, the AppleScript dialog stop icon:

display dialog "Hello" with icon stop

AppleScript dialog with stop icon

Finally, the AppleScript dialog caution icon:

display dialog "Hello" with icon caution

AppleScript dialog with caution icon

Summary

I hope these AppleScript custom dialog tips have been helpful. 

Comments

Permalink

This is a great tutorial thanks, easy to understand too!

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.