Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Tutorial for a "menu bar" app?

Tutorial for a "menu bar" app?
Thread Tools
Forum Regular
Join Date: Feb 2005
Status: Offline
Reply With Quote
Nov 25, 2005, 01:09 PM
 
I'm looking to do a simple app in the menu bar, up near the clock (I don't know the official name therefore I can't google or anything).

Anyone have a good resource or the actual name of the menu/title bar area?

Thanks
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Nov 25, 2005, 01:29 PM
 
There are two slightly different things that go in that area. One type is menu extras, which are what the clock, volume control and other right-side menus provided by Apple are. The menu extra API is private and unsupported, though some people make them anyway. The supported way for developers to get menus up there is through the Cocoa NSStatusItem class. The documentation for this class should give you a good idea what you're doing.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Nov 25, 2005, 11:57 PM
 
My last reply apparently didn't register, so I'm trying again.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Senior User
Join Date: Oct 2004
Location: Portugal
Status: Offline
Reply With Quote
Nov 29, 2005, 04:40 AM
 
This example will help you get started (it's a menu app that allows you to open URLs)


1. Make sure you have the Xcode developer tools.
2. Create a new cocoa project.
3. In Interface Builder, create an instance of an NSMenu.
4. Edit the menu how you want it to look. (I set the font sizes two sizes less then standard)
5. Create a new subclass of NSObject.
6. Instantiate the class.
7. In the inspector, create actions for each menu item. Then create an NSMenu outlet.
8. Connect the menu items to their actions. Connect the outlet to the NSMenu instance.
9. Create files for the class you created.
10. Go back to Xcode.
11. Edit yourClass.m (or whatever you named it)
12. Right after the #import "yourClass.h", add this bit:

Code
@interface NSStatusBar (NSStatusBar_Private)

- (id)_init;
- (void)_insertStatusItem:(id)fp8 withPriority:(int)fp12;
- (id)_lockName;
- (id)_name;
- (void)_refreshWindows;
- (void)_removeStatusItem:(id)fp8;
- (void)_setLengthOfStatusItem:(id)fp8 to:(float)fp12;
- (void)_setUpdatesDisabled:(BOOL)fp8;
- (id)_statusItemWithLength:(float)fp8 withPriority:(int)fp12;
- (BOOL)_updatesDisabled;

@end


13. For each action, I used something like this:

Code
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:@"http://yoursite.com"]];


So each action would look like this:

Code
- (IBAction)centralAnnounce:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:@"http://yoursite.com/1"]];
}


Then, just before the @end line at the bottom of yourClass.m, I added this:

Code
- (void)awakeFromNib
{
NSStatusBar *systemStatusBar = [NSStatusBar systemStatusBar];

if ([systemStatusBar respondsToSelector: @selector(_statusItemWithLength:withPriority:)]) {
statusItem = [[systemStatusBar _statusItemWithLength: 28 withPriority: 8001] retain];}

[statusItem setHighlightMode:YES];
[statusItem setImage:[NSImage imageNamed:@"MenuIcon.tiff"]];
[statusItem setAlternateImage:[NSImage imageNamed:@"MenuIconAlt.tiff"]];
[statusItem setMenu:theMenu];
[statusItem setEnabled:YES];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isPastUser"]==NO)
{
[NSApp activateIgnoringOtherApps:YES];
int status;
status = NSRunAlertPanel(@"Welcome to myMenu",@"This is the first time you have launched myMenu. Would you like to set myMenu to start when you login? This is the only time this message will appear.",@"OK",@"No Thanks",nil);
if ( status == NSAlertDefaultReturn )
{
NSAppleScript *addScpt = [[NSAppleScript alloc]
initWithSource:@"tell application \"System Events\"\nif (count of (login items whose name is equal to \"myMenu\")) is not 0 then\nreturn \"Already installed\"\nend if\nset the_file to (file of application processes whose name is equal to \"myMenu\")\nif (count of the_file) is 0 then\nreturn \"Couldn't locate application\"\nend if\nend tell\n\nset the_path to POSIX path of first item of the_file\n\ntell application \"System Events\"\nmake new login item at the front of login items with properties {path:the_path}\nend tell"];

[addScpt executeAndReturnError:nil];
}
if ( status == NSAlertAlternateReturn )
{}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isPastUser"];
}
}


14. Edit Info.plist and InfoPlist.strings for version numbers, etc.
15. Add an application icon, add menu icons named "MenuIcon.tiff" + "MenuIconAlt.tiff".
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:44 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2