PDA

View Full Version : Application running from toolbar?




brentley
Apr 16, 2012, 03:19 AM
I want to know how I can write an application similar to f.lux and Dropbox that runs straight from the upper-righthand tool bar (not from the dock). Where/what should I read about in order to understand how to run an app that:

1. Runs from the toolbar in the upper-righthand corner
2. Runs all the time, even after reboot

Thanks.



Xcallibur
Apr 16, 2012, 10:19 AM
1. Runs from the toolbar in the upper-righthand corner
2. Runs all the time, even after reboot

Thanks.

This isn't a definitive answer more of a few hints, since I don't currently develop using Objective-C/OSX SDK. But I would think that although the application is running in the toolbar, it is still running, so is like any other application. So hiding the application from the dock whilst using a specific OSX API command for displaying a context menu from the toolbar may satisfy the first requirement.

The second, is the setting of an applications automatic run status, in Windows, you add values to the registry which equate to applications to be run at the startup of the system. So in OSX, there is probably a property that can be set using the OSX SDK to add the application to the list of applications to run at the startup of the system.

Maybe someone more familiar with OSX programming can give you more specific information.

anedwar
Apr 17, 2012, 09:44 AM
Been a while since I had to do this but the basic steps are.

1. Get the system status bar
2. Tell the status bar to give you a status item
3. Create a new menu and assign it to the status item

so something like the following would probably work.

NSStatusBar *systemStatusBar = [NSStatusBar systemStatusBar];
NSStatusItem *test = [systemStatusBar statusItemWithLength: NSVariableStatusItemLength];
[test setTitle: @"Hello"];

NSMenu *testMenu = [[NSMenu alloc] init];
[testMenu setAutoenablesItems: YES];
[testMenu addItem: [[NSMenuItem alloc] initWithTitle: @"1" action: @selector(menuItemPressed:) keyEquivalent:@""]];
[testMenu addItem: [[NSMenuItem alloc] initWithTitle: @"2" action: @selector(menuItemPressed:) keyEquivalent:@""]];

[test setMenu: testMenu];

to make your application run in the background just assign the "Application is background only" property in your application info.plist

To get the application to run at start up have the user add a "Login Item" for you application. I'm sure there is a way to do this programmatically but I have not done so.

To do this manually go to System Preferences -> Users and Groups
Select the user you want and click the "Login Items" tab.

thundersteele
Apr 17, 2012, 11:08 AM
The following tutorial helped me when I did it the first time:
http://cocoatutorial.grapewave.com/2010/01/creating-a-status-bar-application/

Some things are a bit outdated (no ARC), but it's easy to fix that.