Developer

Using AppleScript to build Mac OS X applications

AppleScript is commonly used to automate Mac OS desktop applications. You can easily build applications that use the Aqua UI and interact with other applications on your Mac. We'll show you how.


AppleScript is commonly used to build Mac OS desktop applications. Unlike other scripting languages, its syntax and structure closely resemble spoken English. It's very popular and easy to use; plus, it's a great way to design small programs that automate workflow and increase productivity.

The integration of AppleScript and the Safari Web browser is of particular interest to Web developers. For example, Apple implemented the do Javascript command to allow AppleScript to interact with the JavaScript Document Object Model within Safari. AppleScript can also be used to create client-side programs that can access Web Services using SOAP.

This article will present various tools you can use to design AppleScript applications. We will also dissect a simple program that integrates the Finder and the Safari Web browser.

AppleScript development tools
Designing AppleScript applications is relatively simple. Most of the development tools are freely available on the Apple developer Web site or within the operating system itself. Here are two tools to get you started.

Xcode IDE
Xcode is Apple's free professional IDE specifically designed to code OS X applications. You can obtain Xcode either in the Panther retail box or through an online download (please note that registration at Apple.com is required to obtain these tools). If you've purchased a Mac recently with Panther preinstalled, the Xcode installer package is probably already on the hard drive so you don't need to download it.

Script Editor
This utility is fully integrated in the latest version of Mac OS X. You can usually find it within the Applications folder, inside the AppleScript directory. Script Editor (Figure A) is especially useful for designing quick AppleScript applications on the fly. The sample Safari program featured in this article was written using the Script Editor.

Figure A
Script Editor


Designing your first AppleScript application
We have designed a simple program which taps into the functionality of both Finder and Safari. It is very easy to create your own custom programs that can pass data from one scriptable Mac OS X application to another. Some of these scriptable applications include Address Book, iDVD, iPhoto, Finder, Quicktime and third-party programs such as QuarkExpress and Adobe Acrobat. You can even create custom programs for your iPod! To get detailed information on AppleScript's syntax and structure, be sure to refer to the official language guide available in PDF format on the Apple developer Web site.

Our application will accept a calculation from the user, then send the data to Google's Web site. Google has the ability to solve math problems—it's one of the search engine's more obscure features. Visit Google's help pages for more information about the calculator.

The first thing our custom application does is generate a dialog box to accept the user's input. AppleScript is able to generate a wide variety of windows and boxes using the Aqua interface. Figure B is a screenshot of the dialog box presented to the user.

Figure B
Aqua data-entry box


Once the user inputs the calculation, the application will launch Safari and send the user's query to the Google Web site. Figure C is a screenshot of the end result.

Figure C
Calculation from data-entry box sent to Safari and Google


The actual code
Now let's take a look at the code behind the application. The first thing you need to do is declare calc as a global variable and give it a blank value. The double quotes imply that the value of calc is a string:
set calc to ""

Then you create a tell block and specify that you want to use the Finder script library to generate a dialog box with a user prompt. The user's input is then stored by default in the calc variable:
tell application "Finder"
    display dialog "Enter your calculation:" default answer calc  

The dialog box contains one button with a value of OK. Keep in mind that you can add as many buttons as you wish on a dialog window by enumerating each button value in an array between the curly brackets:
        buttons {"OK"} default button 1
    set calc to text returned of the result

The next thing you want to do is replace every instance of a plus sign (+) with an URLEncoded version of the plus sign (%2B). When you pass values in the QueryString, Google interprets two keywords separated by a plus sign as a concatenated value. The ampersand (&) and the plus sign (+) are commonly used to separate lists of variables and values in querystrings. If you want Google to interpret the plus sign as a mathematical operator, the plus sign has to be URLEncoded. searchReplace is a custom function created to do that very task:
    set calc to (my searchReplace(calc, "+", "%2B"))

Once you've received the user input and replaced all the plus signs, you can then activate the Safari browser. You can programmatically send a URL to the browser, specifically Google's search page. In the QueryString, you append the user's input. The Safari browser will launch automatically, perform the calculation, and display the results:
    tell application "Safari"
        activate
        open location  
            "http://www.google.com/search?&q=" & calc
    end tell
end tell

The searchReplace function takes three arguments: the original string you want to manipulate, the characters you are searching for within the string, and the characters that will replace them. Notice that this custom function is located outside the tell application blocks. The function uses native AppleScript functionality and does not need to rely on a particular OS X application to do its job. The function simply delimits the text according to the search string and replaces each instance of the search string with the replacement string:
on searchReplace(origStr, searchStr, replaceStr)
    set old_delim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to searchStr
    set origStr to text items of origStr
    set AppleScript's text item delimiters to replaceStr
    set origStr to origStr as string
    set AppleScript's text item delimiters to old_delim
    return origStr
end searchReplace

Simple but powerful
AppleScript's ease of use can best be illustrated by this example: You can develop "text-to-speech" applications or integrate voice response in your Mac programs with a single line of code!
say <your text>

Thanks to a powerful library of functions, an attractive user interface, and easy scripting language, AppleScript is a great entry point for any developer wanting to create new applications on the Mac platform. To learn more, visit Apple's official AppleScript site to get started.

Editor's Picks