Some applications (e.g. OmniOutliner) will highlight text when you perform a Spotlight search and open a particular file.
e.g. Performing a Spotlight search for “chocolate” and opening a matching OmniOutliner document will result in “chocolate” appearing in the search box. The result is the document shows you where the terms you were looking for exist.
The way to achieve this functionality is through Apple Events, as described under the “Open” section of the following page:
The code to achieve this (in case Apple decides to remove that documentation) should go into the application:openFiles: method in your NSApplication delegate:
NSString *searchString = [[[[NSAppleEventManager sharedAppleEventManager] currentAppleEvent] paramDescriptorForKeyword:keyAESearchText] stringValue];