我能够使用ApplescriptObjC创建一个小型菜单栏应用程序,它工作正常,菜单显示,我也可以使用不同的图标。现在我一直试图区分右键和左键,以便根据按下的按钮触发不同的操作。我想要做的是当按下鼠标右键时显示菜单,当按下左键时,触发一个处理程序,如:
on leftButtonPressed_(sender)
display dialog "Left Button Pressed!"
end leftButtonPressed_
最终它将被更复杂的东西取代。到目前为止我所做的是创建一个自定义类,其中包含所需的所有ObjC代码,以使Menubar应用程序正常工作。这是:
MenuBarAppAppDelegate.h:
#import <Cocoa/Cocoa.h>
@interface MenuBarAppAppDelegate : NSObject {
NSMenuItem *MenuItem;
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSImage *menuIcon;
NSImage *menuIconActive;
}
@end
MenuBarAppAppDelegate.m
#import "MenuBarAppAppDelegate.h"
@implementation MenuBarAppAppDelegate
-(void)dealloc
{
[menuIcon release];
[menuIconActive release];
//[statusItem release];
[super dealloc];
}
- (void)awakeFromNib
{
statusItem = [[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength]
retain];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
menuIcon= [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Icon1a" ofType:@"png"]];
menuIconActive= [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Icon2a" ofType:@"png"]];
[statusItem setImage:menuIcon];
[statusItem setTitle:@""];
[statusItem setHighlightMode:YES];
[statusItem setEnabled:YES];
[statusItem setToolTip:@"Togglr"];
[statusItem setMenu:statusMenu];
}
-(void)actMenuIcon{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
menuIcon= [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"AppleLogoFullBlack" ofType:@"png"]];
menuIconActive= [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"AppleLogoFullWhite" ofType:@"png"]];
[statusItem setImage:menuIcon];
[statusItem setAlternateImage:menuIconActive];
[statusItem setHighlightMode:YES];
}
-(void)desMenuIcon{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
menuIcon= [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"AppleLogoEmptyBlack" ofType:@"png"]];
menuIconActive= [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"AppleLogoEmptyWhite" ofType:@"png"]];
[statusItem setImage:menuIcon];
[statusItem setAlternateImage:menuIconActive];
[statusItem setHighlightMode:YES];
}
@end
我现在的问题是我不知道如何实际拦截右键和左键鼠标,以及如何将其绑定到applescript处理程序。任何人都可以帮助我吗?
提前致谢!