AppleScript_ The Definitive Guide - Matt Neuburg [242]
set r to do shell script ("perl " & perlScriptPath & " " & f)
feedbackBusy(false)
set L to paragraphs of r
set half to (count L) / 2
set L1 to items 1 thru half of L
set L2 to items (half + 1) thru -1 of L
displayResults( )
on error
feedbackBusy(false)
beep
end try
end doTheSearch
If the doTheSearch handler doesn't error out, it calls displayResults to populate the Results window and present it to the user. Example 27-7 shows the displayResults handler, along with two event handlers connected with the Results window. The double clicked event handler responds when the user double-clicks a line of the table of article titles: the corresponding URL is sent to the web browser for display. The should close handler works around a bug in Tiger's version of AppleScript Studio (at least I think it's a bug): a window that might be shown again later must not be closed, as if it is shown again later it will malfunction. Therefore when the user attempts to close the Results window we prevent it (by returning false) and hide the window instead; it looks to the user as if the window has closed, but it hasn't.
Example 27-7. The displayResults handler
on displayResults( )
tell table view 1 of scroll view 1 of window "results"
set contents to L2
end tell
show window "results"
end displayResults
on double clicked theObject
try
open location (item (clicked row of theObject) of L1)
end try
end double clicked
on should close theObject
hide theObject
return false
end should close
This completes the development of our application. To test it, choose Build → Build and Run in Xcode. (Figure 27-5 shows the running application in action; we've performed a search for articles by our favorite author mentioning our favorite subject, and the first article found is being displayed in a web browser in the background.) To prepare your application for public release, choose Project → Set Active Build Configuration → Release; then choose Build → Clean All Targets and then Build → Build. The result is a more compact application that will run on other users' machines, with the script saved as run-only to hide it from prying eyes.
Figure 27-5. SearchTidBITS in action
Automator Actions
An Automator action (see "Automator" in Chapter 2) is an excellent way to wrap some AppleScript code with a lightweight interface. An Automator action is not a standalone application; rather, it is hosted by Automator (or by some other environment that can run Automator workflows). An action typically has no windows or menus; rather, a single pane (technically, an NSView) appears as the action's interface within Automator, and optionally can appear when the workflow runs, as a way of supplying the script with parameters. This isn't much interface, but in many cases it will be just enough. The script also receives as a parameter the output values from the previous action in the workflow. An Automator action thus gives the end user more power and flexibility than a pure script: the end user can position the action within a larger workflow, and can set options in its interface, effectively repurposing the script and customizing its behavior without seeing or editing its code (and without having to know any AppleScript). To write an Automator action is not difficult, and takes only a few minutes; and it can be done using AppleScript Studio.
Here's a hands-on tutorial illustrating the process of writing an Automator action. (See /Developer/ADC Reference Library/documentation/AppleApplications/Conceptual/AutomatorConcepts for Apple's full documentation.) For our example, we'll write an action that accepts file aliases and encodes those files as MP3s using lame (http://lame.sourceforge.net). It is assumed that lame is installed in its default location, which is /usr/local/bin. In our action's interface, we'll provide an option for selecting a preset and a bitrate. MP3 encoding is a time-consuming activity, so our implementation will script the Terminal (rather than calling do shell script) so the user can see some feedback as the encoding