[ Team LiB ] |
21.2 Some Scriptable ApplicationsThe purpose of this section is to alert you to some useful scriptable applications that are part of Mac OS X but which you might not otherwise be aware of. 21.2.1 FinderOkay, you're probably aware of the Finder. After all, you use it every day, and it is the favorite target application for examples in this book. But I just wanted to remind you about it one more time. The Finder concerns itself with the hierarchy of folders and files on your hard drive. It's very good at such things as renaming files, copying files, deleting files, creating folders and aliases, and describing the folder hierarchy. If you're an old AppleScript hand, accustomed to scripting the Finder for other purposes, you'll want to read about System Events, later in this section. 21.2.2 Internet ConnectIf you connect to the Internet via modem or PPPoE, Internet Connect is a good way to query and manipulate your connection. A useful incantation is: tell application "Internet Connect" to get properties of status The result is a record containing valuable information. For example, if its state is 0, you're not connected to the Internet; if its state is 4, you are. tell application "Internet Connect" set r to properties of status if state of r is 4 then display dialog "You're connected!" end tell 21.2.3 System EventsIn 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, since the Finder wasn't really responsible for this other functionality; it was being used as a kind of stand-in for the System itself. In Mac OS X, scripting of such functionality 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:
System Events is also responsible for GUI scripting (Chapter 22) and for folder actions (Chapter 24). It is thus a very important application, and its dictionary deserves study. Examples of scripting System Events appeared in Chapter 1 and Chapter 2. 21.2.4 Speech Recognition ServerThis application, hidden away in the Carbon framework, is the scriptable frontend for the System's built-in speech recognition functionality. For an example, see Section 20.5.2. 21.2.5 URL Access ScriptingThis is a background-only application in the ScriptingAdditions folder. When it was invented (back in Mac OS 8.5, I believe), it was a very good thing, because it provided a way to download and upload via http and ftp across the Internet without the overkill of having to drive a full-featured client such as Netscape or Fetch. However, I personally find it undependable, and since we're now in the Unix-based world of Mac OS X, I recommend the use of do shell script and curl instead (see Chapter 23). 21.2.6 Keychain ScriptingThis is another background-only application in the ScriptingAdditions folder, and acts as a scriptable frontend to the user's keychain, where passwords are stored. This is analogous to the functionality accessed through the Keychain Access utility. Keychain Scripting lets a script fetch a password from the keychain instead of hardcoding it in plaintext. For example: tell application "Keychain Scripting"
set k to (get key 1 of keychain 1 whose name is "mail.mac.com")
tell k to set L to {name, account, password}
end tell
L -- {"mail.mac.com", "mattneub", "teehee"}
If, as in that example, you ask for the password of a key whose access control is set to require confirmation, the Confirm Access to Keychain dialog will appear (possibly twice—once for the Keychain Scripting application and once for the host application in which the script is running). You cannot script changes to the access control rules for a key through Keychain Scripting; I take it that this is a security measure. 21.2.7 Image EventsAs this book was being written, Apple had begun work on a background-only application, Image Events, living in the same directory as the Finder and System Events. It will probably allow you to perform some basic manipulations on image files, such as scaling, rotating, and manipulating color profiles, as well as supplying information about monitors. For example: tell application "Image Events" set f to (path to desktop as string) & "bigImage.eps" set f2 to POSIX path of (path to desktop) & "/smallerImage.eps" set im to open file f scale im by factor 0.5 save im in f2 end tell |
[ Team LiB ] |