Reputation: 13
I want to add icons on the left side of my status bar menus text in OS X. I cannot find a sample code to implement this in my code. Right now i am using the code to set the image for the status bar (below) and IBActions to call AppleScript files.
- (void)awakeFromNib {
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
NSBundle *bundle = [NSBundle mainBundle];
statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wifi1" ofType:@"png"]];
statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wifi2" ofType:@"png"]];
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setMenu:statusMenu];
[statusItem setHighlightMode:YES];
}
- (void)dealloc {
[statusImage release];
[statusHighlightImage release];
[super dealloc];
}
- (IBAction)RemoteAppleEvents:(id)sender {
NSString* path = [[NSBundle mainBundle] pathForResource:@"AppleEvents" ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];
}
- (IBAction)InternetSharing:(id)sender {
NSString* path = [[NSBundle mainBundle] pathForResource:@"WiFiShare" ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];
}
- (IBAction)BluetoothSharing:(id)sender {
NSString* path = [[NSBundle mainBundle] pathForResource:@"bluetooth" ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];
}
Any help would be appreciated. Thanx!
UPDATE*
My drop down menu in IB
.
Upvotes: 1
Views: 1978
Reputation: 6932
Updated answer:
To set a icon in a NSMenuitem or NSMenu like this:
You simply have to add an image to your project and set the menus image in the "Attribute Inspector"
If you want to set the image programatically:
Give each Menu item a outlet and then use the
setImage:
As you have done for the Status bar
bar = [NSStatusBar systemStatusBar];
statusItem = [bar statusItemWithLength: NSVariableStatusItemLength] ;
statusImage = [NSImage imageNamed:@"status.icns"];
statusHighlightImage = [NSImage imageNamed:@"statusHighLight.icns"];
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setMenu:_statusMenu];
[statusItem setHighlightMode:YES];
menu1Image = [NSImage imageNamed:@"login.icns"];
menu2Image = [NSImage imageNamed:@"persist.icns"];
menu3Image = [NSImage imageNamed:@"thumbIcon_.png"];
[_MenuItem1 setImage:menu1Image];
[_MenuItem2 setImage:menu2Image];
[_MenuItem3 setImage:menu3Image];
Also my code take into account ARC
Upvotes: 2