Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [230]

By Root 1649 0
technically and, as it were, morally: an application that "sees" what characters I'm typing into FrameMaker at this moment could justly be accused of spying. Nevertheless, in recent versions of Mac OS X the range of observable application-internal events has been greatly increased, to include not simply when an application is activated or hidden, but also when a menu item is chosen, when a window is created, moved, resized, or miniaturized, as well as what happens at an even more detailed level of the application's interface. This means that your script can react to such internal events occurring in a particular application.

The mechanism here is the Accessibility API, just as for GUI scripting (see Chapter 24), except that it's being used in reverse. When an item in an appropriately written application's user interface is affected, the application gives off a little squawk called a notification. A second application can register to receive such notifications, and so it is effectively observing what goes on in the first. Not every application can be observed in this way; but every Cocoa application can, at the least. To experience the full power of this observation mechanism, try PreFab's UI Actions .

An example used in the tutorial included with UI Actions is a modification of Script Editor (I can't think how else to express it—attachability really does, in effect, modify an application's behavior) so that every time a new (untitled) script window is created, a comment is inserted giving the current date:

tell application "UI Actions"

tell UI action "Script Editor-AXWindowCreated"

set d to date string of (get timestamp)

set w to affected UI element

end tell

end tell

tell application "System Events"

set n to title of w

if n begins with "Untitled" then

tell application "Script Editor"

set text of document n to "-- " & d

end tell

end if

end tell

As with folder actions (see earlier in this chapter), UI Actions lets you associate an observed event in an application with a script that should be executed when that event occurs. So, using UI Actions, the user will "attach" this script to Script Editor's "AXWindowCreated" notification, so that when a new window is created in Script Editor, the script will be called. When the script is called, it targets UI Actions to get a reference to the window that generated the notification; then it targets System Events to learn (through GUI scripting) the name of this window; finally, if this appears to be a new script window (because its name starts with "Untitled"), it enters the current date into the window.

UI Actions can be an elegant solution to the problem of making sure that your script is triggered at an appropriate moment. The UI Actions application itself is scriptable, so you could write a script that creates a script and attaches it to a particular action, or that enables or disables a particular attached script. In fact, such a script could itself be triggered through being attached to some action. The mind boggles.

Chapter 27. Writing Applications


You might wish to create a standalone application, perhaps as a way of distributing your script to other users in a form that's easy to use. AppleScript provides the simplest way in the world to write an application: just save your script as an applet . An applet is a true standalone application; it can even accept drag-and-drop of files and folders onto its icon, and (most surprising) it's scriptable. However, an applet has essentially no interface (except for display dialog and other user-interactive scripting addition commands). You can wrap your script in a full-fledged interface, with windows, buttons, text fields, menus, and similar bells and whistles, using AppleScript Studio, a free development environment that lets you create a Cocoa application even if the only programming language you know is AppleScript. Another use of AppleScript Studio is to wrap your script in the smaller interface of an Automator action; users can then customize it through its interface and link it to other actions to create

Return Main Page Previous Page Next Page

®Online Book Reader