searchMenuTemplate and search field

i have an nsmenu and a search field connect by searchMenuTemplate. the menu shows up but whenever i click something in the nsmenu i get an NSReceiverEvaluationScriptError 3. There isnt event anything in the choose menu item handler…by the way i’m trying to recreate the search field in itunes. The documentation include virtually no information on this, besides making a recent searches menu, which works. But for some reason this doesn’t.

EDIT: now i’m this far,

on awake from nib theObject
call method "setSearchMenuTemplate:" of (text field "text" of window "main") with parameter theObject
end awake from nib

That works, but i still get an error when i click a menu item.

if it helps anyone, i just read that this problem happened to someone with a popup button, but the way they fixed it was setting choose menu item for the popup button instead of each menu item, but i can’t do that with an NSMenu.

anyone? i can’t find any answers online anywhere.

Hi fiftyfour,

No idea why the AppleScript handler for the menu does not work, but I had an idea for a workaround - I tried it and it worked here:

here is how to:

  1. in Interface Builder connect your searchfield with the menu using Ctrl-Drag and ‘searchMenuTemplate’ (should also work programmatically)

  2. add a hidden text field somewhere and connect it to the ‘on changed’ handler

  3. connect every menu item with this text field Ctrl-Drag and ‘takeIntValueFrom:’

  4. give every menu item an idividual tag (1, 2, 3, 4 …)

  5. subclass NSMenuItem and set the subclass as custom class of every menu item

and here is the subclass:

[code]// MyMenuItem.h
#import <Cocoa/Cocoa.h>

@interface MyMenuItem : NSMenuItem
{}
-(int)intValue;

@end[/code]

[code]// MyMenuItem.m

#import “MyMenuItem.h”

@implementation MyMenuItem
-(int)intValue{
NSTextField *tf = [self target];
[tf setIntValue:[self tag]];
[[NSNotificationCenter defaultCenter] postNotificationName:NSControlTextDidChangeNotification object:tf];
return [self tag];
}

@end[/code]
now the ‘on changed’ handler of your script gets called every time you select a menu item and you can ask the text field for it’s tag :slight_smile:

D.

thanks, that works great. There’s only one problem, how would i change the state of the clicked menu item to check and uncheck all the other ones.

hmm - you could add some lines in the intValue method:

[code]-(int)intValue{
NSTextField tf = [self target];
[tf setIntValue:[self tag]];
[[NSNotificationCenter defaultCenter] postNotificationName:NSControlTextDidChangeNotification object:tf];
NSArray
menuitems = [[self menu] itemArray];
NSMenuItem *item;
NSEnumerator *enumerator = [menuitems objectEnumerator];
while(item = [enumerator nextObject])
[self isEqual:item] ? [item setState:NSOnState] : [item setState:NSOffState];

return [self tag]; 

}[/code]

wow, it works. thanks, i gotta learn obj-c.