oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Programming with Spotlight
Pages: 1, 2, 3, 4

Our controller class needs outlets and connections, so let's add those. Replace your controller files with the following ones. (We're simply adding an outlet that'll correspond to a button and an action that contains a Carbon-level call to open up Spotlight's search window.)

For "Controller.h":

//  Controller.h
//  SpotlightExamples

#import <Cocoa/Cocoa.h>

@interface Controller : NSObject {
    IBOutlet NSButton* openSearchWindowButton;    

- (IBAction)openSearchWindowAction:(id)sender;

For "Controller.m":

//  Controller.m
//  SpotlightExamples

#import "Controller.h"

@implementation Controller

- (IBAction)openSearchWindowAction:(id)sender
    OSStatus resultCode=noErr;
    //Replace "Search Text" with user input
        HISearchWindowShow((CFStringRef)@"Search Text", kNilOptions);
    if (resultCode != noErr) {
        NSLog(@"Failed to open search window");
        //Could use NSAlert class to display interactive dialog

Let's go ahead and instantiate the controller.

  • Instantiate the Controller
    • Open up the main menu of your application by expanding the "NIB Files" folder and double clicking on "MainMenu.nib"
    • Drag the "Controller.h" file in Xcode down onto Interface Builder's "MainMenu.nib" panel.
    • In the "Classes" tab of Interface Builder's "MainMenu.nib" panel, select "NSObject" and then "Controller"
    • From Interface Builder's "Classes" menu, choose "Instantiate Controller"

IB Main Palette
Drag and drop your "Controller.h" file onto Interface Builder's main palette and then instantiate it.

You should now see your controller under the "Instances" tab of Interface Builder as a blue cube. The little yellow exclamation point reminds us that at least one outlet is not set, so let's take care of that.

  • Add a button and make the connections
    • From Interface Builder's "Cocoa-Controls" palette, drag an NSButton onto your application's main menu window. Double click and rename it "Open Search Window"
    • Ctrl-click and drag from the blue controller cube onto the button; release and choose "Connect" for the "openSearchWindowButton" outlet.
    • Ctrl-click and drag from the button onto the blue controller cube; release, and choose "Connect" for the "openSearchWindowAction" action.

Now you can run the application, click on the button, and get the search window to appear. As you can see, there's just a simple Carbon-level call to make this happen. To make this feature useful, your application might offer drag-and-drop functionality to allow a user to drag over a file for some sort of processing.

Your initial project can display the Spotlight search window.

You can get the project file for this first portion here.

Pages: 1, 2, 3, 4

Next Pagearrow