View Full Version : Open and fill the Find Window

Aug 23, 2006, 08:20 AM
I'm doing a project based on Cocoa. I'm almost done, it's a little project, but I want to know how my application could open the Find window (Command-F) and set the string to search.


Aug 23, 2006, 10:09 AM
Sadly there is no NSFindPanel; it's actually part of the NSTextView functionality so check out the docs for that if that's applicable. Otherwise you're rolling your own. If you decide to do the latter then the (fairly old but still valid) code here (http://www.stone.com/The_Cocoa_Files/Doing_Objects_Right.html) should help.

Aug 23, 2006, 12:39 PM
Actually, you can do this in a fashion by manually setting the value that you want to appear in the find panel on the NSFindPboard. This is what applications use so that when you find something in one application and switch to another, its find panel will show the same string.

The code that caveman_uk linked to actually has a good example of how to do this.

- (void)loadFindStringToPasteboard {
NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
[pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteboard setString:[self findString] forType:NSStringPboardType];

However, doing this has the probably undesirable effect that the find string will be overwritten for other applications as well, even if the user doesn't search for something using your find panel.

Aug 23, 2006, 01:47 PM
Great! I have a NSTextView actually. I look at the code on stone.com.
First I saw the PastBoard thing and added it. Then I kept digging and implement the find/findstring combo and it works!

My app is only to browse source code faster while I'm doing investigations (many many projetcs and files, and they're not XCode projects).

Aug 23, 2006, 02:54 PM
If you have an NSTextView then take a look at the class reference. You might not need to write your own. There are methods in NSTextView to support the use of a find panel.

Aug 24, 2006, 09:40 AM
My first though was to use a find panel to go to the text I want to search.
But what I have now is better. :) I have a NSTableView fill with function names and a source file path were I can see how the function is used. When I clicked on a function name, I load the source file in the NSTextView and scroll down to the funtion I want to investigate.

My only bug is when I click on a column header to sort the NSTableView,
the name of the function does not match the path name. :( The TableView
is bound to an ArrayController.

- (IBAction) fOpenSourceButtonClicked: (id)sender
//get the row selected in the scroll view
int irow = [scrollview selectedRow];
//get the columns of the scroll view
NSArray * aColumns = [scrollview tableColumns];

//get the source path column (index = 1)
NSTableColumn * tcolpath = [aColumns objectAtIndex:1];
//get the selected cell for the source path
NSTextFieldCell * tfcell = [tcolpath dataCellForRow:irow];
//get the source path text
NSString * strPath = [tfcell stringValue];

//get the function name column (index = 0)
NSTableColumn * tcolfuncname = [aColumns objectAtIndex:0];
//get the selected cell for the function name
NSTextFieldCell * tftosearch = [tcolfuncname dataCellForRow:irow];
NSString * strToSearch = [tftosearch stringValue];