How do I build menu bar apps on macOS using Swift?

If you're looking to build a menu bar app on macOS using Swift, you'll want to familiarize yourself with the AppKit framework. Below is a simple example of how to create a basic menu bar app that displays an icon and a menu.

import Cocoa @main class AppDelegate: NSObject, NSApplicationDelegate { var statusItem: NSStatusItem! func applicationDidFinishLaunching(_ aNotification: Notification) { // Create the status item and set the icon statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) if let button = statusItem.button { button.image = NSImage(systemSymbolName: "star", accessibilityDescription: "Star Icon") button.action = #selector(showMenu) } // Create the menu let menu = NSMenu() menu.addItem(NSMenuItem(title: "Item 1", action: #selector(menuItemAction), keyEquivalent: "1")) menu.addItem(NSMenuItem(title: "Item 2", action: #selector(menuItemAction), keyEquivalent: "2")) menu.addItem(NSMenuItem.separator()) menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate), keyEquivalent: "q")) statusItem.menu = menu } @objc func showMenu() { statusItem.menu?.popUp(positioning: nil, at: NSPoint(x: 0, y: 0), in: nil) } @objc func menuItemAction(sender: NSMenuItem) { print("\(sender.title) clicked") } }

macOS menu bar apps Swift AppKit NSStatusItem development macOS programming