Skip to content

Instantly share code, notes, and snippets.

@4np
Last active March 9, 2017 22:15
Show Gist options
  • Save 4np/6cf9c530c46b4eada0f0ac4350064758 to your computer and use it in GitHub Desktop.
Save 4np/6cf9c530c46b4eada0f0ac4350064758 to your computer and use it in GitHub Desktop.
OS X Menu Bar Application

How to quickly create a menu bar application

Add a status bar icon

Download this icon and drag it into your Assets.xcassets.

Add a status item

AppDelegate.swift

//
//  AppDelegate.swift
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    lazy var statusItem: NSStatusItem = {
        let item = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)
        
        if let button = item.button {
            button.image = NSImage(named: "StatusBarButtonImage")
            button.action = #selector(showMenu)
        }
        
        return item
    }()
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // set up status item
        let _ = self.statusItem
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
    
    // MARK: Handle menu
    
    func showMenu() {
        debugPrint("show menu...")
    }
}

Remove the application icon

add entry

Remove the initial controller

uncheck

That's the Gist of it. If you need to know more about how to present a menu or a window this guide may help you get further ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment