Status Bar App
How to create a status bar app using Xcode and Swift on Mac OSX.

The following example has been tested using Xcode 6.1.1 on Mac OS X 10.10.1 . But it may likely works fine on other versions as well. Your comments or ideas on how to simplify this further are more than welcome!

See also the Objective-C version.

Create a new empty Project

From Xcode create a new OS X project of type "Cocoa Application":



Set up the MainMenu.xib interface

To start with you may remove the default Window object (you won't need it for this project):



Then you can add (ctrl+drag) a new Menu object to the application:



Please do not remove the existing default "Menu" object from the application, otherwise keyboard shortcuts as ⌘C (copy) and ⌘V (paste) won't work anymore throughout the application!

Set the description and the shortcut key of the last menu option to "Quit" and use ⌘Q as a shortcut key:



Change the implementation of the app delegate

Add a statusBarMenu outlet and a statusBarItem variable to the AppDelegate.swift file:

class AppDelegate: NSObject, NSApplicationDelegate {
   @IBOutlet weak var statusBarMenu: NSMenu!
   var statusBarItem: NSStatusItem!
   


Add the following code in the applicationDidFinishLaunching method of the AppDelegate.swift file:
func applicationDidFinishLaunching(aNotification: NSNotification) {
{
   statusBarItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1.0)
   statusBarItem.menu = statusBarMenu
   statusBarItem.title = "It's me!"
   statusBarItem.highlightMode = true
}

Make all necessary connections

First, add the "terminate:" response to the "Quit" menu item. To do this, ctrl+drag from the menu item to the first responder and then choose option "terminate:" from the drop down list menu that will appear:





Then connect the outlet in the AppDelegate.swift with the menu object. Simply ctrl+drag from the menu object to the IBOutlet variable:



Change info.plist

Set the application as an agent app, so that it won't appear in the Dock or in the Force Quit window. To do this add the key Application is agent (UIElement) to the application info.plist file and set it to YES:



Run the application

As you run the application from Xcode it should immediately show on the Mac status bar:



Tips

Use an icon, in place of text, to be shown on the status ba.
Add the following lines of code in the applicationDidFinishLaunching method of the AppDelegate.swift file:

statusBarItem.title = "";
statusBarItem.image = NSImage(named: "StatusIconNormal")
statusBarItem.alternateImage = NSImage(named: "StatusIconHighlighted")


  • Xcode Project Download

Gaetano Causio © | Privacy Policy | Disclaimer