Sanniv Khare 6 min read

From NSStatusItem to MenuBarExtra: porting a Mac menu bar app

From NSStatusItem to MenuBarExtra: porting a Mac menu bar app

A menu bar app written with NSStatusItem and NSMenu can move to SwiftUI’s MenuBarExtra with less code, but plan for two deployment-target jumps: MenuBarExtra needs macOS 13, and SettingsLink, the simple way to open a Settings window from the menu, needs macOS 14. The other change that catches people: MenuBarExtra has no menuNeedsUpdate(_:) moment, so a menu that rebuilds itself on every click has to keep its data current another way. We built the same small utility both ways on our M1 lab Mac with Xcode 26.2 (Swift 6.2.3, macOS 26.2 SDK, macOS 14.0 deployment target); both apps built cleanly, and the shared model’s tests pass.

Why we did this: OneClick QuitAll, our Mac app that lists running apps and quits the ones you do not keep, lives in the menu bar. Every menu bar utility eventually faces the same choice between AppKit and SwiftUI, so we built a small sample that does what QuitAll’s menu does and ported it.

What we built, twice

Both versions share one model and offer the same three things:

  • a list of running apps from NSWorkspace.shared.runningApplications, filtered to regular apps and sorted by name, each with a “keep” checkmark;
  • a Quit the others command that sends a normal quit (NSRunningApplication.terminate(), so apps can ask to save) to everything except the kept apps, Finder and itself;
  • a Settings window.

The AppKit version (StatusItemLab) uses NSStatusBar.system.statusItem(withLength:), an NSMenu with a delegate, and an NSWindow hosting a SwiftUI settings view. The SwiftUI version (MenuBarLab) is a MenuBarExtra scene plus a Settings scene. Treat the code below as a starting point to adapt to your own app.

The menu, side by side

The same menu bar menu written with NSStatusItem and NSMenu, and with MenuBarExtra

AppKit rebuilds the menu on each click; SwiftUI describes it once and redraws when the model changes.

In AppKit, the status item owns an NSMenu and the delegate refills it in menuNeedsUpdate(_:), which Apple describes as “invoked when a menu is about to be displayed”. Each item needs a target, an action and a state. In SwiftUI the menu is views: Toggle becomes a checkmark item, Divider a separator, Button a command.

@main
struct MenuBarLabApp: App {
    @StateObject private var model = RunningAppsModel()

    var body: some Scene {
        MenuBarExtra("Quit apps", systemImage: "xmark.circle") {
            ForEach(model.apps) { app in
                Toggle(app.name, isOn: keep(app))
            }
            Divider()
            Button("Quit the others") { model.quitOthers() }
            SettingsLink { Text("Settings…") }
                .keyboardShortcut(",")
        }
        .menuBarExtraStyle(.menu)

        Settings { SettingsView() }
    }
}

What maps one to one, and what does not

Built from Apple’s MenuBarExtra, NSStatusItem and SettingsLink pages, and checked against our two builds:

  • Creating the item. NSStatusBar.system.statusItem(withLength:) plus button.image becomes MenuBarExtra(_:systemImage:content:) (or image: for an asset). Apple notes the title “will be used by the accessibility system”.
  • Menu items. NSMenuItem with state becomes Toggle; a plain item becomes Button; NSMenuItem.separator() becomes Divider().
  • Popover instead of a menu. An NSPopover shown from the status button becomes .menuBarExtraStyle(.window), which Apple describes as “a popover-like window from the menu bar icon”.
  • Hiding the Dock icon. NSApp.setActivationPolicy(.accessory) in code becomes the LSUIElement key, which Apple recommends for apps that only show in the menu bar. In a project with a generated Info.plist, the build setting INFOPLIST_KEY_LSUIElement = YES writes it; our built MenuBarLab.app shows "LSUIElement" => 1.
  • Letting the user remove the item. NSStatusItem.behavior becomes the isInserted: binding: “If the user removes the item from the menu bar, the binding will be set to false.” Without that binding, Apple says “an app that only shows in the menu bar will be automatically terminated if the user removes the extra from the menu bar.”
  • Settings window. A hand-made NSWindow becomes a Settings scene opened by SettingsLink, which “opens the window for the scene or orders it to the front if it is already open”. macOS 14 and later.
  • Refreshing on open. menuNeedsUpdate(_:) has no equivalent in the MenuBarExtra API. Keep the model current instead (below).
  • No equivalent: keep AppKit. Right-click menus, a custom view in the menu bar (NSStatusItem.view), or different actions for click and Option-click: the MenuBarExtra initializers take a label and content, nothing else.

Where the deployment target bites

We type-checked the SwiftUI app against three deployment targets with the Swift 6.2.3 compiler from Xcode 26.2:

swiftc output: MenuBarExtra needs macOS 13, SettingsLink needs macOS 14

Real compiler output from our M1 lab Mac. At macOS 14.0 the app type-checks with no errors.

If your app still supports macOS 12, either raise the deployment target or keep the AppKit status item until you can. If you only need macOS 13, replace SettingsLink with a button that opens your own settings window.

Keeping the menu current without menuNeedsUpdate

The AppKit menu calls model.refresh() each time it opens. With MenuBarExtra the content is driven by the model, so the model has to update itself. Ours listens to NSWorkspace launch and quit notifications:

@MainActor
public final class RunningAppsModel: ObservableObject {
    @Published public private(set) var apps: [AppEntry] = []
    @Published public var kept: Set<String> = []
    private var observers: [NSObjectProtocol] = []

    public init() {
        refresh()
        let nc = NSWorkspace.shared.notificationCenter
        for name in [NSWorkspace.didLaunchApplicationNotification,
                     NSWorkspace.didTerminateApplicationNotification] {
            observers.append(nc.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in
                Task { @MainActor in self?.refresh() }
            })
        }
    }
}

The same model serves both apps, which is the main gain of the port: the logic moves into a package you can test without any UI. Our RunningAppsKit package runs three XCTest cases (the quit list skips the app itself, Finder and kept apps; an empty keep list quits everything else; the live list comes back sorted), and all three pass.

One AppKit trap with the Swift 6.2 compiler

The AppKit version starts with a custom @main entry point that holds the app delegate. Our first attempt failed to compile, even in Swift 5 language mode:

error: call to main actor-isolated initializer 'init()'
in a synchronous nonisolated context
    static let delegate = AppDelegate()

AppDelegate is @MainActor, and the static property that creates it was not. Marking the entry type @MainActor fixed it:

@main
@MainActor
enum StatusItemMain {
    static let delegate = AppDelegate()
    static func main() {
        let app = NSApplication.shared
        app.setActivationPolicy(.accessory)   // no Dock icon
        app.delegate = delegate
        app.run()
    }
}

A SwiftUI App avoids this, since the framework owns the entry point.

Build results

Clean Release builds of both apps: sizes, build times, LSUIElement and tests

Both apps from one clean Release build each on our M1 lab Mac.

Both apps built in about 6 seconds from empty derived data, with code signing off. The AppKit app came to 164 KB and the SwiftUI app to 180 KB. These are one build of a small sample on one Mac, not a benchmark; for a menu bar utility, size and build time are not a reason to pick either side.

What to check in your own app

  1. Decide the deployment target first: 13 for MenuBarExtra, 14 if you want SettingsLink.
  2. Move the menu’s data into an ObservableObject (or @Observable on macOS 14) that updates itself, and put it in a package with tests.
  3. Replace setActivationPolicy(.accessory) with LSUIElement in the Info.plist.
  4. Add an isInserted: binding if users should be able to hide the item without the app quitting.
  5. Keep AppKit for anything the MenuBarExtra API does not offer: right-click menus, custom status bar views, per-click behaviour.

If your menu bar app also needs to start at login, our port from SMLoginItemSetEnabled to SMAppService covers that side.

Made by the team behind this post

Apps from our studio

  • Goes with this post

    Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Goes with this post

    OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get
  • Goes with this post

    Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.