Random Access   chris, kp & rob
Tech Take-Apart
Coding Your Mac
by kp

Last month, I explored the power of OS X's Automator application. This program makes custom scripting very easy with its pre-loaded functions and drag-n-drop interface. Now, I am going to investigate how to develop your own AppleScripts and outline features of XCode, Apple's application tool.

ScriptEditorAppleScript has been part of the Mac OS since System 7 and was designed to control Apple Events. If you are already familiar with programming, AppleScript is a simple scripting language that is easy to learn. To start writing Applescript, you need a basic understanding of programming principles. Last month, I recommended Sams Teach Yourself AppleScript in 24 Hours which gives you quick lessons on AppleScript programming basics. Another good reference is AppleScript for Dummies that uses a straightforward approach to learning AppleScript. Once you have the basics mastered, you can compose scripts with ease.

Within Mac OS X there are several tools available to build AppleScripts. For this example, I am going to use the Script Editor which is found in Applications -> AppleScript -> Script Editor. Before writing the script, here is a quick tour around the Script Editor interface.

The first thing that you see is the editing window that has three parts. At the top is a list of buttons - Record, Stop, Run and Compile. Record can create corresponding AppleScript as you do actions, Stop halts the script's process, Run begins the script, and Compile debugs and converts code. You can add additional buttons to the menu by going to View -> Customize toolbar. There are a few other settings you may want to change before starting to code. Under the Preferences menu, there are options for editing and formatting. For editing, it is better to have the" Wrap Lines" and "Use Script Assistant" checked for help while composing scripts. In the Formatting menu, you can set custom defaults to mark keywords, comments, variables, etc. These settings come in handy when you compile your code, and the specific elements have their own identifying features.

Other important sections of Script Editor are the Composing Window and the Description, Result and Event Log tabs. The Composing Window is where you type in your AppleScript. The Description tab allows you to type in details about your script, the Result tab shows the answers or calculations for operations, and the Event Log keeps track of the script's actions. Another important menu is the Library window (Window -> Library) which shows a list of all AppleScript-compatible applications. This menu gives you a set of commands for each application. If an application is not listed, you can test to see if it can use AppleScript by dragging the application icon onto the Script Editor icon. If the application is compatible, the Library opens automatically, if not, a warning message displays.

Now that you are orientated with the Script Editor application, it is time to learn a few simple AppleScript commands. To demonstrate, I've created a simple script using some common commands. The "tell application" statement is used to direct a command to an application; in this case the Finder. Next, I want to show a dialog box with text, so I indented the command "display dialog". After the dialog box displays and "OK" is selected, a second command is set in motion to open Safari. The "activate" command brings the Safari window forward and opens the specified URL with the "open location" command. To finish, I closed each "tell" command with "end tell". If you have the Script Assistant on, Script Editor can help you compose your code while you write, and in addition, the compiler corrects mistakes or displays a warning. When in doubt, use the Library search feature to look up the definition and function of a command.
Here is a sample script:

--activate this script in the startup items
tell application
      display dialog
             "Good Morning, KP. Today is " & (current date) & "." & " Here is today's technology news ..."
      tell application
             open location
      end tell
end tell

To use the script for yourself, copy and paste the above text into Script Editor and modify the name and web URL location. Next, save the script with the .scpt extension. Finally, activate this script as part of the startup items through your user account. Go to System Preferences -> Accounts -> Your Account -> LogIn Items, then click the plus (+) to attach the script. Don't forget to check the item to activate upon startup.

XcodeFor more advanced programming, Apple has its own IDE application called Xcode. It's a collection of development software for building native OS X applications. It consists of Project Builder, AppleScript Studio and Interface Builder. Xcode is compatible with C, C++, Java and AppleScript. These tools are available as part of your OS X system software but are not installed with your general system software. You need a copy of the Apple Developer Tools disks to install Xcode on your Mac.

Some good references to start with Xcode are Mac OS X Developer’s Guide and Mac OS X Command Line: Unix Under the Hood. Both of these books have plenty of step-by-step instructions and code examples to get you programming fast. Until next month, happy scripting!

Shop Online:
AppleScript for Dummies
Mac OS X Developer’s Guide
Mac OS X Command Line: Unix Under the Hood
Sams Teach Yourself AppleScript in 24 Hours

Apple's AppleScript Tutorial
Apple Developer Connection
Mac OS X Bible, Tiger Edition

Get Random Access

Understanding Tech

Print this article

Shop Online


Your Name:

Your Email:

Your Friend's Name:

Your Friend's Email:

 © Micro Center