Balloons icon
We're celebrating our birthday!
Get 6 courses for $20 →
Advertisement

AppleScript: Creating Complex Dialogs with Ease

by

AppleScript is an incredibly natural and intuitive scripting language that helps you automate tasks in OS X. I'm quite fond of the syntax and how easy it is to learn and write, but there are definitely some areas that are harder to pick up than others. For instance, I always have difficulty remembering all of the options and specific syntax involved in creating dialog windows such as those that allow the user to input some text or make a selection from a number of options.

Today we're going to take a quick look at how you can streamline this process with an incredibly handy tool from the Mac App Store called Dialog Maker.


Learn AppleScript

Don't know AppleScript? This article may not be much good to you. However, you can learn almost everything you need to know from just two other articles here on AppStorm!


Dialogs: The Hard Way

To see how this process normally works, let's try a test case. Let's say we want to create a dialog that allows the user to choose one or more of the days of the week. You might start with code something like this:

[applescript]
choose from list {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
[/applescript]

Simple enough right? This dialog will work, but it takes advantage of many of the default settings that we may or may not want to use. For instance, by default, the user can only select one option. At this point, I usually hit up Google to see what options accompany a typical dialog. It turns out that we can add in a ton of different things, here are a few examples.

A Title:

[applescript]
with title "Calendar Picker"
[/applescript]

A Prompt (instructions):
[applescript]
with prompt "Choose one or more days"
[/applescript]

Custom Buttons:
[applescript]
OK button name "These Days" cancel button name "No Days"
[/applescript]

A Default Selection:
[applescript]
default items {"Monday"}
[/applescript]

Enable Multiple Selections:
[applescript]
with multiple selections allowed
[/applescript]

Once we figure out all these various options, we can then combine them into one super long line of code:

[applescript]
choose from list {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} with title "Calendar Picker" with prompt "Choose one or more days" OK button name "These Days" cancel button name "No Days" default items {"Monday"} with multiple selections allowed
[/applescript]

Granted, as far as high level programming goes, this isn't exactly complicated stuff. However, for new coders this can be a pretty cumbersome chunk of code to work with. Further, as I already mentioned, sometimes the most difficult part is simply remembering all of the options that you have with each type of dialog and how to implement them correctly.

AppleScript Dictionaries help immensely but an even easier option is to use an app called Dialog Maker, which provides you with a simple, form-like interface for building complex dialogs in AppleScript. Let's take a look at the three different types of dialogs that it helps you build.


Display Dialog

The first of the three options in Dialog Maker is "Display Dialog". This is the simplest of the three and provides the user with a message and up to three possible responses in the form of buttons or text entry.

Let's say we want to create a titled dialog with simple instructions and three possible responses in the form of buttons. First, we open up Dialog Maker and select the "Display Dialog" option along the top. From here we can use a series of basic controls instead of writing code by hand:

Dialog Maker
Dialog Maker

As you can see, the first thing we're asked to do is provide an optional title for the dialog. I chose the "text" option and typed in a title. Next up are the text entry options, since I just wanted to go with a simple button-driven dialog, I skipped these.

Finishing the Dialog

From here, we simply go along and fill out the rest of the options. First, you can choose whether or not you want an icon (select your own or a default option). Next, you choose how many buttons you want (up to three), what each says and what the default behaviors and selections will be.
My favorite option here is the ability to have Dialog Maker set up your "if" statement for you. Most of the time you're going to want certain actions to happen when a certain button is clicked, which is accomplished through "if" and "then" commands.

Finishing the Dialog
Finishing the Dialog

Previewing and Inserting

Once you're all finished filling out the forms, you can click the "Preview" button to see your dialog in action. A live, interactive version of the window will then appear.

Previewing the Dialog
Previewing the Dialog

Once you're satisfied with the results, you can either copy the code to your clipboard or click the "Insert" button have it automatically appear in the active AppleScript Editor window.

Inserting the Dialog
Inserting the Dialog

Choose From List

With the "Choose From List" section, we can create the dialog that we started the article with in seconds with no fuss. Just as with the previous section, we have instant access to all of the pertinent options such as title, prompt, etc.
One of the things that I really like here is the system for adding items to the list: each item appears as a little unit that can be easily rearranged, deleted, etc. As you add items in this section, the one below it automatically updates so you can choose the default option.

Adding List Options
Adding List Options

As you can see in the preview below, we were able to build the exact same dialog with all the options that we used before, only this time instead of typing the code manually we simply manipulated a few controls and pasted in the text.

Choose From List Dialog
Choose From List Dialog

Choose File

The final option is one that you're definitely familiar with and no doubt encounter several times an hour on your Mac: a file dialog. Here you get a lot of great options for filtering the content that appears in the window, an option that I had never really even explored until using this app!

Choose File Dialog
Choose File Dialog

Conclusion

Coding dialogs by hand in AppleScript can be a bit of a pain. Not only do you have to remember lots of various snippets of code that are required to achieve the effect that you want, you also have to remember what all the default settings are for each dialog and how to change them.

Using an app like Dialog Maker can really speed up your coding process and help you through one of the trickier portions of AppleScript. Also, since the app generates code for you to insert, you'll learn so much every time you use it that you'll find yourself needing the app less and less!

Advertisement