Building a Basic AppleScriptObjC (Cocoa-AppleScript) Application with Xcode
In OS X Mountain Lion, AppleScript continues to be a powerhouse tool for controlling applications on the Mac. Whether you need to automate the creation of a few folders in the Finder, or generate a several-hundred-page product catalog in Adobe InDesign, AppleScript is waiting to lend a hand. Despite AppleScript's learning curve, Mac users everywhere are using it on a daily basis, and it saves time and money that might otherwise be wasted.
AppleScripts aren't known for having slick interfaces. Perhaps most often, these scripts are written as simple applications, using the AppleScript Editor (found in /Applications/Utilities). When you double-click the application, it launches, performs some series of hidden tasks, and then quits. Developing script applications that work like this is quick and easy if you have the know-how, but if you're planning to distribute your script to other people, this format isn't always ideal. For one thing, aside from displaying simple dialog messages, you have no real way to let users know what your script is doing. Furthermore, unless users are AppleScript-savvy, there's no way for them to adjust the behavior of your script.
AppleScriptObjC (also called Cocoa-AppleScript) is an advanced method of developing AppleScript-based native Cocoa applications. With AppleScriptObjC, you can develop rich user interfaces and interact with them directly from your scripts. You can also tie directly into the Cocoa API, triggering methods and interacting with classes. Essentially, AppleScriptObjC allows you to develop AppleScript-based applications that can do almost anything a Cocoa application can do, in addition to all of the normal inter-application automation capabilities of a typical AppleScript. This means that you can create full-featured AppleScript-based applications, complete with windows, progress bars, and virtually any other interface elements you can imagine.
Since AppleScriptObjC is a very complex topic, this tutorial starts with the basics. We'll walk through the creation of a very simple "Hello World" application, designed to demonstrate some of the core concepts of AppleScriptObjC. You can build on these techniques to develop more advanced applications that serve real-world purposes.
Novice AppleScripters are certainly welcome to follow along, but please be aware that AppleScriptObjC has a fairly steep learning curve. Therefore, I recommend that you have the following:
- Working knowledge of AppleScript. You should be relatively familiar with core AppleScript syntax, as I don't cover it in the tutorial. At the end of this article, I suggest some resources for getting started with AppleScript.
- Basic knowledge of Cocoa (Objective-C). This is always a good idea.
- Mac OS X 10.6 or later. AppleScriptObjC was released with Snow Leopard, and it isn't available in earlier operating systems.
- Xcode. If you don't already have Xcode, look for it in the Mac App Store (see Figure 1).
Figure 1 Xcode in the Mac App Store.