AppleScript

From Mac Guides

Jump to: navigation, search

AppleScript is a scripting language developed by Apple Computer, which uses English-like syntax.

Contents

Description

Many people dismiss AppleScript as an inferior language due to its inability to do certain things, and due to its English-like syntax which programmers of other languages can find restricting. However they are often misinformed. While AppleScript is not designed for making full-fledged applications (although AppleScript Studio is bridging this gap), it specializes in communicating with and manipulating other, "scriptable" applications. For example, you could create a script to gather song information from iTunes and send it to a buddy in iChat. This script would be communicating with two different applications, something which is difficult if not impossible to do using other languages.

History

AppleScript was first introduced in October 1993 with the Mac OS 7.1.1 update, made possible by the addition of Apple Events to the operating system in Mac OS 7. QuarkXPress was one of the first applications to support it, and its AppleScript support is used as an explanation as to why the Mac remained popular in the publishing market even after Quark was ported to Windows.

Small but relatively frequent AppleScript updates have been made since its introduction, adding new language features. However, AppleScript support was sometimes difficult to add to applications, so many developers avoided doing so. A significant change came with the release of Mac OS X, when Cocoa developers could implement AppleScript support in a much simpler way than ever before, resulting in an increase in support for the language.

Editors

Most AppleScript developers edit their scripts with Script Editor (located at /Applications/AppleScript/Script Editor), which is developed by Apple and bundled with Mac OS X. There are also several third party editors.

If you have Apple's Developer Tools installed, you can use Xcode to create AppleScript applications. Using the Xcode suite, you can create custom interfaces to use for your AppleScript application. This requires a lot more background knowledge than Script Editor but is also more powerful.

For users who find the AppleScript language difficult, Automator is another alternative. Introduced in Mac OS X 10.4 (Tiger), it allows you to set up "workflows" without writing any code. Automator can be more limiting than AppleScript.

Dictionaries

For AppleScript to communicate with an application, the application must have an AppleScript dictionary. These dictionaries list all classes, properties and commands supported by the application. The dictionaries can be viewed from within Script Editor by choosing the "Open Dictionary..." command from the File menu, then choosing an application.

Language Examples

Hello World

The first example is a very simple one that displays a dialog displaying the traditional "Hello World" message. It demonstrates the English-like syntax that AppleScript uses.

display dialog "Hello World" buttons "OK" default button "OK"

The above example uses the "display dialog" command" with several parameters. Other parameters, such as "with icon" and "default answer", also exist but are always optional.

Current Track

This example is slightly more complicated in that it communicates with another application. This is done with a "tell" block. It also uses variables, which store data such as numbers, text, and dates, and demonstrates string concatenation. You will also see in the example that comments can be written by placing "--" at the start of a line.

The example will get the name of the current track from iTunes and display it in a dialog.

--Get the name of the current track from iTunes
tell application "iTunes"
	set trackName to name of current track
end tell

--Concatenate strings into what we will show in the dialog
set message to "The current track is: " & trackName
--Show a message stating the current track
display dialog message buttons "OK" default button "OK"

Shell Scripting

One powerful way to use AppleScript, is in conjunction with shell scripting. Although this requires some knowledge of shell scripting, it often lets you do things that AppleScript can't do by itself.

This example lets you play an audio file in the background:

do shell script "afplay audiofile.wav"

Links

Apple's main AppleScript page

Adding AppleScript support to your application