AppleScript_ The Definitive Guide - Matt Neuburg [239]
The Search window contains an NSForm displaying three fields the user can search on (text, title, and author) and a Search button, along with a spinning progress indicator to provide feedback while we're talking to the Internet (Figure 27-1).
Figure 27-1. Search window
The Results window contains a single-column table for displaying the titles of the found articles, along with some explanatory text telling the user what to do (Figure 27-2).
In the menu bar, I remove the File menu and replace it with a Search menu consisting of a single New menu item; also, I add a Close menu item to the Window menu. Finally, I run through the menu items of the application menu and the Help menu and replace the placeholder "NewApplication" with "SearchTidBITS," the name of our application (Figure 27-3).
Now comes the really interesting part. As you'll recall, the Cocoa framework defines certain action messages, lifetime notifications, and delegation queries that can be sent to your code. In Interface Builder, we must specify which of these messages we want to receive, with respect to each element of our interface as well as the application as a whole. (Remember, we must elect to receive some of these messages or our code will never run.) At the same time, we should also give at least some of our interface elements AppleScript names, so that our AppleScript code can refer to them. All of this is done in the AppleScript pane of the Inspector window.
Figure 27-2. Results window
Figure 27-3. The menu bar
Figure 27-4 shows the process. I've selected the Search window (not shown) and now I focus my attention on the Inspector and its AppleScript pane. In the Name field I've entered an AppleScript name, "search"; this will allow our code to refer to this window by name, as window "search". In the list of event handlers , I've checked the "will open" checkbox; this means that our code will receive a will open event at the appropriate moment in the lifetime of this window (namely, when it is about to open). Somewhat confusingly, it is not sufficient to check this checkbox; it is also necessary to check the Script checkbox specifying the file SearchTidBITS.applescript, so that AppleScript Studio knows what script to send the will open event to (even though our project has in fact only one script).
Figure 27-4. AppleScript Inspector for the Search window
Here are the interface items of our project and the treatment I apply to each:
File's Owner
This icon in the main Interface Builder window represents the application as a whole. I want to know when the application has launched so that I can perform some initializations, so I check Application: launched (a lifetime notification).
Search → New menu item
I want to know when the user chooses this menu item, so I check Menu: choose menu item (the menu item's action message).
Window → Close menu item
I want the frontmost window to close when the user chooses this menu item. I could check Menu: choose menu item for this menu item as well, but the knowledge of how to close a window is already built into Cocoa, so there is no need to involve AppleScript or any code at all. Instead, I form a Cocoa connection. To do so, I Control-drag from this menu item to the First Responder icon in the main Interface Builder window, and connect it to the First Responder's performClose: method.
Search window
I name this window search. I want to know when this window is about to open, because I want to make some interface adjustments; so I select Window: will open (a lifetime notification).
Search window: the Search button
I want to know when the user clicks this button, so I select Action: