AppleScript_ The Definitive Guide - Matt Neuburg [212]
Some Scriptable Applications
As part of the default Tiger installation, Apple ships a number of applications with varying degrees of useful scriptability . Some of these are in places where you might think to look, but a number of them are not, so here's a quick survey of the scriptable applications you'll find on your computer. (Third-party applications, which are often the most important scriptable applications in a workflow, are not discussed here.)
iApps and Utilities
Of the applications in your /Applications directory, quite a number are usefully scriptable. iTunes is probably the most popular target; it is, perhaps, the only one of the iApps whose powers emerge to their fullest extent only in the presence of AppleScript. Address Book and iCal are essentially databases, and provide scriptable access to their data. Mail is scriptable, though there are many bugs and functionality holes.
Safari , aside from control over its preferences and interface, responds to just one important command, do JavaScript, but this command should not be underestimated, as it opens the door to some powerful capabilities. System Profiler is another one-trick pony: you can retrieve an XML version of its report, from which you can retrieve any desired information. Script Editor is scriptable enough to allow insertion of templated control structures through its contextual menu. If you see the Internet via modem or PPPoE, Internet Connect is a good way to query and manipulate your connection; you can connect and disconnect, and learn whether you are connected. iChat is scriptable enough to let you get buddy list information and send a message. TextEdit's scriptability is substandard. Other applications that are scriptable in a rudimentary way are Automator, DVD Player, Font Book, iPhoto, iSync, and QuickTime Player.
Finder
The Finder, located in /System/Library/CoreServices, is the favorite target application for examples in this book—and with good reason. For all its faults, Finder scripting is a solid way to interact with the hierarchy of files and folders on your hard disk. It's very good at such things as renaming files, copying files, deleting files, creating folders and aliases, and describing the folder hierarchy. The Finder is also one scriptable application that is almost certain to be running (so that, for example, you have somewhere to start when targeting a remote machine).
System Events
In Mac OS 9 and before, the Finder was the locus of scriptability for a lot of system functionality that had nothing to do with files and folders, such as what applications were running. This was somewhat irrational, as the Finder wasn't involved in such matters; it was being used as a kind of stand-in for the system itself. In Mac OS X, scripting of system functionality of this kind has been moved to a faceless background application called System Events (located in the CoreServices folder, along with the Finder). Here are some of the things you can do with System Events:
Sleep, restart, and shut down the computer.
Manipulate login items and access users:
tell application "System Events"
get full name of current user
make new login item at end of login items with properties ¬
{path:"/Applications/Safari.app"}
end tell
Navigate the hard disk hierarchy. The object model is similar to the Finder, but not identical; you have to start with a disk or with a domain:
tell application "System Events"
name of folder 1 of desktop folder of user domain -- "Mannie"
end tell
Manipulate running processes, determining such things as what processes are running, which ones are visible , and which one is frontmost .
Perform elementary parsing of XML and property list files.
Explore QuickTime file attributes.
Here we examine some features of an audio file:
tell application "Finder" to set f to (get file 1 as string)
tell application