macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button AppleScripting Mac OS X

An Introduction to AppleScript on Mac OS X

12/11/2001

Look out everyone--AppleScript is coming back and coming back big!

AppleScript has been one of the most overlooked, cool technologies lurking in the Mac OS since 7.1, but it's usually hidden in your Apple Extras folder waiting to be discovered. Now with Mac OS X, it has a new home in the Applications folder, indicating that Apple no longer considers it just an extra. AppleScript has a bright future, and there's no better time than now to take advantage of this amazing technology.

"Why," you might ask, "should we once again see what AppleScript has to offer?" I can give you three reasons: Cocoa, Unix, and AppleScript itself.

The Cocoa advantage

Creating a Scriptable Application has never been easier, and Cocoa is what brings this into reality. Cocoa is an object-oriented framework that holds information and the methods that access this information within objects. Cocoa applications are really just a collection of these objects working together to accomplish a task.

Related Reading

AppleScript in a NutshellAppleScript in a Nutshell
By Bruce W. Perry
Table of Contents
Index
Sample Chapter
Full Description

Each of these Cocoa objects and their methods can potentially be accessed through AppleScript, and many of the core scripting commands are gained "for free." Planning for scriptabilty with Cocoa leads naturally to good program design. Even better, if you have an existing program written using Cocoa, adding AppleScript support is probably easier than you think.

The Unix way

Unix is usually seen as an OS, but one of the strengths of Unix is the ability to combine together many small programs to build a larger program that does what you want, all from the command line.

So, you can have one program read the contents of a file, send it to a stream editor to replace one string with another, and then break it into fields and store those fields with formatting in another file. Typically, Unix does this with very small programs that have a very specific purpose (i.e., to read the contents of a file); however, most programs on Macs and Windows are more monolithic, taking care of many functions that are usually related to each other (browsing, sending email, word processing, transferring files, etc.).

Since Cocoa typically has objects as the basis of these functions, scripting will allow access to those objects and their methods and data individually. Now a user can combine them with functions from other apps, the OS, and AppleScript itself, making them work more like the Unix model and giving them great flexibility and power.

AppleScript X

AppleScript has matured along with the Mac OS, and AppleScript 1.7 found in Mac OS X 10.1 allows for new ways of doing things that are incredibly convenient.

Many applications that come with Mac OS X are already scriptable, including the Finder, iMovie, Internet Connect, iTunes, Mail, Print Center, QuickTime, Sherlock, and Terminal. Also, Mac OS X has made running scripts incredibly easy by adding Toolbar scripts and an optional Script Menu that lets you run AppleScripts, Perl scripts, and shell scripts from anywhere. With these additions, starting a work flow is as easy as navigating through your files and clicking a button or selecting a menu item.

Additionally, AppleScript can now access Web Services. These are XML-based services that use SOAP or XML-RPC to send information to your scripts over the Internet. This gives you access to updated information or live feeds of data, like stock quotes, temperatures, and currency conversions, among others.

If this isn't enough to reaffirm your belief in Apple's commitment to AppleScript, Apple has just released AppleScript Studio, which makes AppleScript a peer language with ObjC and Java for building Cocoa applications, including front ends built with Interface Builder. Clearly, AppleScript has never been better. If you'd like to see some of this for yourself, go to Apple's AppleScript Web site and take a look at the Seybold 2001 AppleScript demo. Very cool stuff.

AppleScript for everyone

Comment on this articleAs this series gets underway, we're curious about what you'd like to see us cover. Let us know.
Post your comments

If you haven't used AppleScript before, you will be happy to hear that it compares very well with other scripting languages on other platforms. Windows uses Visual Basic (VBScript) as the basis of its scripting support, whereas Linux or other Unix OSes usually use shell scripts or Perl. While these are not the only options for these OSes, they are the most common ones. Each scripting system has its own advantages and disadvantages, but in my opinion, none are as accessible to the non-programmer as AppleScript.

AppleScript uses a nice, human, language-like syntax that is user-friendly and easy to read and understand. It even supports local dialects, so you can write scripts in English or French or German, for example, adopting that language's syntax and idioms.

An example script (from Apple) to hide all Finder windows looks like this:

tell application "Finder"
    if the (count of windows) is not 0 then
    set collapsed of every window to true
    end if
end tell

AppleScript supports record-ability, where a user's actions can be captured as a script, automatically. AppleScript can work between Native and Classic applications on Mac OS X 10.1 and between different machines via the Internet. It has a gentle learning curve, but it can scale very well to accomplish advanced tasks. In all, this means that AppleScript has the potential to be used by not only hardcore Mac programmers, but by everyday users too. Add this ease of use to Cocoa's ability to access individual functions within programs, and you have the makings of something great.

Your first scriptable Cocoa application

Hopefully, you're now a believer in scripting and can't wait to add it to your applications. Just to show you how easy it really is, I'm going to show you how to build a scriptable application in four steps.

  1. Open Project Builder and create a new Cocoa application named "ScriptableApp".
  2. Build the project without modifying anything by clicking on the Build Active Target (Hammer) button.
  3. Open the Info.plist, available by opening the Products folder and then the ScriptableApp.app, and add the following to the end of current list of keys:
    
    		<key>NSAppleScriptEnabled</key> 
    		<string>YES</string>
    

    Make sure it is formated like the other keys and comes before the closing </dict> tag.


Figure 1. Adding the NSAppleScriptEnabled key to ScriptableApp's info.plist

  1. Save and rebuild your application. That's it!

You have now added support for the Core Suite of AppleScript commands to this application.

You can verify this by opening up Script Editor inside your AppleScript folder in the Applications folder and selecting "Open Dictionary..." from the File menu.

Since the ScriptableApp has not been installed, it's still located inside your project's build folder. Click on the Browse button and navigate to the ScriptableApp's project folder and look for a build folder. Inside it you should find the ScriptableApp.app. Select it and you should see the ScriptableApp launch and a ScriptableApp Dictionary window come up with the Standard Suite and Text Suite on the left side of the window. Clicking on an item in the left-hand pane will bring up the syntax for that item and a brief description of how that item is used.


Figure 2. Opening ScriptableApp's Dictionary

Now, we haven't added any text objects to our application, so the Text Suite doesn't really do anything in this application. Also, some of the Standard Suite commands, like save, print, move, delete, make, duplicate, need to have more code added before they "do the right thing," but we can access many of the commands and set and get values for different elements right away.

I've written a script that shows off a few of these commands and some of the things we can do "for free." Download ScriptableApp.sit and open it in Script Editor. Run it and review some of the commands for yourself. You might also look through the dictionaries of some other applications, and review the example scripts that come with AppleScript and that are also available on Apple's AppleScript Web site.

Aim of this series

For the rest of this series, I'm going to focus on adding scripting capabilities to Cocoa applications written in ObjC first. I'll also delve into localization, creating GUI-less applications, and native scripting additions.

If you are unfamiliar with ObjC or Cocoa, please check out Mike Beam's excellent series on Cocoa programming. I won't be using advanced examples at first, but casual knowledge of both will be helpful.

Cocoa applications can also be written in Java and we will see how to add support for scripting in those applications. I'll also look at AppleScript Studio, available in the very latest version of the Mac OS X development tools, and see what it brings to the mix.

Next time

In the next article, we will talk about how Cocoa supports scripting at the object level and go into greater detail about the Core Suite and Text Suite we saw in our ScriptableApp example. We will also talk about how to best design programs that make adding scripting easy. See you then.

Brad Dominy is the head of Neoki, LLC, a small web design firm located in Chicago, IL.


Return to the Mac DevCenter.