objective c - How to add icons in os x status bar menus (left from text) -
i want add icons on left side of status bar menus text in os x. cannot find sample code implement in code. right using code set image status bar (below) , ibactions 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 appreciated. thanx!
update*
my drop down menu in ib
.
updated answer:
to set icon in nsmenuitem or nsmenu this:
you have add image project , set menus image in "attribute inspector"
if want set image programatically:
give each menu item outlet , use
setimage:
as have done 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 code take account arc
Comments
Post a Comment