{
  "@context": "https://schema.org",
  "@id": "https://aggr.aymericbeaumet.com/items/steipete-me/2025-06-17-showing-settings-from-macos-menu-bar-items-a-5-hour-journey/#webpage",
  "@type": [
    "WebPage",
    "ArchiveComponent"
  ],
  "articleBody": "![](https://steipete.me/assets/img/2025/showing-settings-from-macos-menu-bar-items/hero.png)\n\nOpening a settings window from a macOS menu bar app should be trivial. It’s not. After spending hours debugging, I’m documenting the gotchas to save you the same frustration.\n\n## The Problem\n\nSwiftUI provides [`SettingsLink`](https://developer.apple.com/documentation/swiftui/settingslink) for opening settings:\n\n```swift\nMenuBarExtra(\"Test\", systemImage: \"star.fill\") {\n    SettingsLink {\n        Text(\"Open settings\") \n    }\n}\n```\n\nSimple, right? Except it doesn’t work reliably in [`MenuBarExtra`](https://developer.apple.com/documentation/swiftui/menubarextra). The documentation doesn’t mention this limitation.\n\nAccording to Apple’s documentation, `SettingsLink` should “open the app’s settings scene when activated.” However, this assumes your app is already active and has proper window management context - assumptions that don’t hold for menu bar apps.\n\n## Why It Fails\n\nMenu bar apps operate differently from regular macOS apps:\n\n- **No dock icon by default** - They use [`NSApplication.ActivationPolicy.accessory`](https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/accessory)\n- **Not “active” in the traditional sense** - They don’t appear in the app switcher\n- **Windows may appear behind other apps** - Without proper activation, windows lack focus\n- **No SwiftUI graph loaded** - Settings needs some SwiftUI view initalized and the menu bar uses AppKit under the hood.\n\nThe root issue is that [`NSApplication`](https://developer.apple.com/documentation/appkit/nsapplication) treats menu bar apps as background utilities, not foreground applications. This affects how windows are ordered and receive events.\n\n## The Evolution of Workarounds\n\n### The Old Way\n\nThis is how it used to work. Private API, but widely used and simple:\n\n```swift\nif #available(macOS 13, *) {\n    NSApp.sendAction(Selector((\"showSettingsWindow:\")), to: nil, from: nil)\n} else {\n    // macOS 12 or earlier\n    NSApp.sendAction(Selector((\"showPreferencesWindow:\")), to: nil, from: nil)\n}\n```\n\nThis stopped working in Sonoma (14) with the error: “Please use SettingsLink for opening the Settings scene.” Apple deprecated these selectors in favor of SwiftUI’s scene-based approach, but didn’t account for the unique challenges of menu bar apps.\n\n### The openSettings Environment Action\n\nApple provides an [`openSettings` environment action](https://developer.apple.com/documentation/swiftui/opensettingsaction) for programmatic access (available since macOS 14.0+):\n\n```swift\nstruct MyView: View {\n    @Environment(\\.openSettings) private var openSettings\n\n    var body: some View {\n        Button(\"Open Settings\") {\n            openSettings()\n        }\n    }\n}\n```\n\nThis currently works on macOS 15, but doesn’t work on macOS Tahoe (26). The logic needs an existing SwiftUI render tree, and simply calling the environment variable does nothing if none is found.\n\nThe workaround? As horrible as it sounds, a *hidden window*. Of course, that comes with its own issues, unless you massage the window that it’s really off-screen and ideally also doesn’t react to touches.\n\n## Hide & Seek\n\nNow, this works, however the window will open in the *background*, and no amount of `makeKeyAndOrderFront(nil)` will help. Trust me. I (and [Claude](https://steipete.me/posts/2025/claude-code-is-my-computer/)) tried plenty variations.\n\nThe real reason? macOS doesn’t allow a window to become selected when there’s no Dock icon. And since it’s common to hide the Dock icon for pure Menu Bar apps, that’s a problem.\n\nThe workaround? Show the Dock icon just before calling `openSettings()` and then hiding it again. In a way, this is also convenient for the user as the Icon now represents the “app” - the visible window, and once that closes, we hide the Dock icon again. (via calling `NSApp.setActivationPolicy(.accessory)`). Of course the whole thing requires some delays to really work, so let me present you the final, working solution (I apologize in advance):\n\n## The Working Solution\n\nHere’s the minimal implementation for macOS 14 and higher, using Swift 6:\n\n```swift\n// Hidden window to provide context\nstruct HiddenWindowView: View {\n    @Environment(\\.openSettings) private var openSettings\n    \n    var body: some View {\n        Color.clear\n            .frame(width: 1, height: 1)\n            .onReceive(NotificationCenter.default.publisher(for: .openSettingsRequest)) { _ in\n                Task { @MainActor in\n                    // Show dock icon for window focus\n                    NSApp.setActivationPolicy(.regular)\n                    try? await Task.sleep(for: .milliseconds(100))\n                    \n                    // Activate and open\n                    NSApp.activate(ignoringOtherApps: true)\n                    openSettings()\n                    \n                    // 2. No window ordering - After openSettings(), you might need additional window management to ensure\n                    // it comes to front:\n                    try? await Task.sleep(for: .milliseconds(200))\n                    if let settingsWindow = findSettingsWindow() {\n                        settingsWindow.makeKeyAndOrderFront(nil)\n                        settingsWindow.orderFrontRegardless()\n                    }\n                }\n            }\n            .onReceive(NotificationCenter.default.publisher(for: .settingsWindowClosed)) { _ in\n                // Restore menu bar app state when settings closes\n                NSApp.setActivationPolicy(.accessory)\n            }\n    }\n}\n\n// Window identifier for settings\nstatic let settingsWindowIdentifier = \"com.apple.SwiftUI.Settings\"\n\n/// Finds the settings window using multiple detection methods\nstatic func findSettingsWindow() -> NSWindow? {\n    // Try multiple methods to find the window\n    return NSApp.windows.first { window in\n        // Check by identifier\n        if window.identifier?.rawValue == settingsWindowIdentifier {\n            return true\n        }\n        \n        // Check by title\n        if window.isVisible && window.styleMask.contains(.titled) &&\n           (window.title.localizedCaseInsensitiveContains(\"settings\") ||\n            window.title.localizedCaseInsensitiveContains(\"preferences\")) {\n            return true\n        }\n        \n        // Check by content view controller type\n        if let contentVC = window.contentViewController,\n           String(describing: type(of: contentVC)).contains(\"Settings\") {\n            return true\n        }\n        \n        return false\n    }\n}\n\n// App structure\n@main\nstruct MenuBarApp: App {\n    var body: some Scene {\n        MenuBarExtra(\"My App\", systemImage: \"star.fill\") {\n            Button(\"Settings...\") {\n                NotificationCenter.default.post(name: .openSettingsRequest, object: nil)\n            }\n            .keyboardShortcut(\",\", modifiers: .command)\n        }\n        \n        // Required Settings scene\n        Settings {\n            SettingsView()\n                .onDisappear {\n                    NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n                }\n        }\n        \n        // Hidden window for context\n        Window(\"Hidden\", id: \"HiddenWindow\") {\n            HiddenWindowView()\n        }\n        .windowResizability(.contentSize)\n        .defaultSize(width: 1, height: 1)\n    }\n}\n\nextension Notification.Name {\n    static let openSettingsRequest = Notification.Name(\"openSettingsRequest\")\n    static let settingsWindowClosed = Notification.Name(\"settingsWindowClosed\")\n}\n```\n\nThe [`NotificationCenter`](https://developer.apple.com/documentation/foundation/notificationcenter) approach decouples the menu action from the window context, allowing the hidden window to handle the actual settings opening.\n\nFor a production-ready implementation with all edge cases (yes, there are some more…) handled, see [VibeTunnel’s SettingsOpener.swift](https://github.com/amantus-ai/vibetunnel/blob/2a63599ce0b09d139ddc9954f41f2a5840264f9f/mac/VibeTunnel/Utilities/SettingsOpener.swift#L4).\n\n## Scene Order Matters (Unfortunately)\n\nWhile implementing @steipete’s solution described above, I (@matejkob) discovered another gotcha: **the order of scenes in your App’s body actually affects whether this workaround functions correctly**.\n\nThe working example shown earlier has the scenes in this order:\n\n1. `MenuBarExtra`\n2. `Settings`\n3. `Window` (hidden)\n\nHowever, I found that if you arrange them like this (which seems equally logical):\n\n```swift\nvar body: some Scene {\n    MenuBarExtra(\"My App\", systemImage: \"star.fill\") {\n        ContentView()\n    }\n    .menuBarExtraStyle(.menu)\n    \n    Settings {\n        SettingsView()\n            .onDisappear {\n                NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n            }\n    }\n    \n    // Hidden window for settings context\n    Window(\"Hidden\", id: \"HiddenWindow\") {\n        HiddenWindowView()\n    }\n    .windowResizability(.contentSize)\n    .defaultSize(width: 100, height: 100)\n}\n```\n\nThe hidden window won’t be “visible” to the SwiftUI environment system (at least not on macOS Sequoia 15.5), and the `openSettings()` call will fail silently. However, moving the hidden `Window` scene **before** the `Settings` scene makes it work perfectly:\n\n```swift\nvar body: some Scene {\n    // Hidden window for settings context - MUST come before Settings\n    Window(\"Hidden\", id: \"HiddenWindow\") {\n        HiddenWindowView()\n    }\n    .windowResizability(.contentSize)\n    .defaultSize(width: 100, height: 100)\n    \n    MenuBarExtra(\"My App\", systemImage: \"star.fill\") {\n        ContentView()\n    }\n    .menuBarExtraStyle(.menu)\n    \n    Settings {\n        SettingsView()\n            .onDisappear {\n                NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n            }\n    }\n}\n```\n\nThis suggests that SwiftUI’s scene resolution and environment propagation happens in declaration order, and the `@Environment(\\.openSettings)` action in the hidden window needs its context established before the `Settings` scene is processed.\n\nWhether this is an Xcode build system quirk, a SwiftUI implementation detail, or intended behavior is unclear from Apple’s documentation. What is clear is that **the hidden window must be declared before the Settings scene** for this workaround to function reliably.\n\nThis adds yet another layer of undocumented complexity to what should be a simple operation - not only do we need a hidden window and activation policy juggling, but we also need to carefully order our scene declarations to avoid mysterious failures.\n\n## Understanding the Workaround\n\nThe hidden window serves multiple purposes:\n\n- Provides a valid window context for the `openSettings` environment action\n- Allows us to temporarily switch activation policies without visual disruption\n- Gives us a place to handle the complex timing orchestration\n\nThe dock icon manipulation (switching between [`.accessory`](https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/accessory) and [`.regular`](https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/regular)) is necessary because macOS only brings windows to the front reliably for apps with dock icons.\n\n## Fin\n\nWhat should be a one-liner in other frameworks requires careful orchestration in SwiftUI. The combination of [`MenuBarExtra`](https://developer.apple.com/documentation/swiftui/menubarextra), [`Settings`](https://developer.apple.com/documentation/swiftui/settings) scenes, and [`openSettings`](https://developer.apple.com/documentation/swiftui/opensettingsaction) wasn’t designed with the unique constraints of menu bar apps in mind.\n\nThis shouldn’t be so hard. Opening a settings window is one of the most basic operations any app needs to perform. The fact that it requires hidden windows, activation policy juggling, and precise timing delays in 2025 is a testament to how menu bar apps remain second-class citizens in SwiftUI. Until Apple addresses these fundamental issues, we’re stuck with these workarounds.\n",
  "authors": [],
  "captured_at": "2026-09-27T19:29:15.293681927+00:00",
  "category": "programming",
  "content_html": "<p><picture class=\"article-picture article-picture-fallback\" style=\"--image-ratio:16 / 9\"><img src=\"https://steipete.me/assets/img/2025/showing-settings-from-macos-menu-bar-items/hero.png\" alt=\"\" loading=\"eager\" decoding=\"async\" fetchpriority=\"high\" referrerpolicy=\"no-referrer\" class=\"progressive-image\"></picture></p>\n<p>Opening a settings window from a macOS menu bar app should be trivial. It’s not. After spending hours debugging, I’m documenting the gotchas to save you the same frustration.</p>\n<h2>The Problem</h2>\n<p>SwiftUI provides <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/swiftui/settingslink\"><code>SettingsLink</code></a> for opening settings:</p>\n<pre><code class=\"language-swift\"><span class=\"code-snippet\" data-language=\"Swift\">MenuBarExtra(&quot;Test&quot;, systemImage: &quot;star.fill&quot;) {\n    SettingsLink {\n        Text(&quot;Open settings&quot;) \n    }\n}\n</span></code></pre>\n<p>Simple, right? Except it doesn’t work reliably in <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/swiftui/menubarextra\"><code>MenuBarExtra</code></a>. The documentation doesn’t mention this limitation.</p>\n<p>According to Apple’s documentation, <code>SettingsLink</code> should “open the app’s settings scene when activated.” However, this assumes your app is already active and has proper window management context - assumptions that don’t hold for menu bar apps.</p>\n<h2>Why It Fails</h2>\n<p>Menu bar apps operate differently from regular macOS apps:</p>\n<ul>\n<li><strong>No dock icon by default</strong> - They use <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/accessory\"><code>NSApplication.ActivationPolicy.accessory</code></a></li>\n<li><strong>Not “active” in the traditional sense</strong> - They don’t appear in the app switcher</li>\n<li><strong>Windows may appear behind other apps</strong> - Without proper activation, windows lack focus</li>\n<li><strong>No SwiftUI graph loaded</strong> - Settings needs some SwiftUI view initalized and the menu bar uses AppKit under the hood.</li>\n</ul>\n<p>The root issue is that <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/appkit/nsapplication\"><code>NSApplication</code></a> treats menu bar apps as background utilities, not foreground applications. This affects how windows are ordered and receive events.</p>\n<h2>The Evolution of Workarounds</h2>\n<h3>The Old Way</h3>\n<p>This is how it used to work. Private API, but widely used and simple:</p>\n<pre><code class=\"language-swift\"><span class=\"code-snippet\" data-language=\"Swift\">if #available(macOS 13, *) {\n    NSApp.sendAction(Selector((&quot;showSettingsWindow:&quot;)), to: nil, from: nil)\n} else {\n    // macOS 12 or earlier\n    NSApp.sendAction(Selector((&quot;showPreferencesWindow:&quot;)), to: nil, from: nil)\n}\n</span></code></pre>\n<p>This stopped working in Sonoma (14) with the error: “Please use SettingsLink for opening the Settings scene.” Apple deprecated these selectors in favor of SwiftUI’s scene-based approach, but didn’t account for the unique challenges of menu bar apps.</p>\n<h3>The openSettings Environment Action</h3>\n<p>Apple provides an <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/swiftui/opensettingsaction\"><code>openSettings</code> environment action</a> for programmatic access (available since macOS 14.0+):</p>\n<pre><code class=\"language-swift\"><span class=\"code-snippet\" data-language=\"Swift\">struct MyView: View {\n    @Environment(\\.openSettings) private var openSettings\n\n    var body: some View {\n        Button(&quot;Open Settings&quot;) {\n            openSettings()\n        }\n    }\n}\n</span></code></pre>\n<p>This currently works on macOS 15, but doesn’t work on macOS Tahoe (26). The logic needs an existing SwiftUI render tree, and simply calling the environment variable does nothing if none is found.</p>\n<p>The workaround? As horrible as it sounds, a <em>hidden window</em>. Of course, that comes with its own issues, unless you massage the window that it’s really off-screen and ideally also doesn’t react to touches.</p>\n<h2>Hide &amp; Seek</h2>\n<p>Now, this works, however the window will open in the <em>background</em>, and no amount of <code>makeKeyAndOrderFront(nil)</code> will help. Trust me. I (and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://steipete.me/posts/2025/claude-code-is-my-computer/\">Claude</a>) tried plenty variations.</p>\n<p>The real reason? macOS doesn’t allow a window to become selected when there’s no Dock icon. And since it’s common to hide the Dock icon for pure Menu Bar apps, that’s a problem.</p>\n<p>The workaround? Show the Dock icon just before calling <code>openSettings()</code> and then hiding it again. In a way, this is also convenient for the user as the Icon now represents the “app” - the visible window, and once that closes, we hide the Dock icon again. (via calling <code>NSApp.setActivationPolicy(.accessory)</code>). Of course the whole thing requires some delays to really work, so let me present you the final, working solution (I apologize in advance):</p>\n<h2>The Working Solution</h2>\n<p>Here’s the minimal implementation for macOS 14 and higher, using Swift 6:</p>\n<pre><code class=\"language-swift\"><span class=\"code-snippet\" data-language=\"Swift\">// Hidden window to provide context\nstruct HiddenWindowView: View {\n    @Environment(\\.openSettings) private var openSettings\n    \n    var body: some View {\n        Color.clear\n            .frame(width: 1, height: 1)\n            .onReceive(NotificationCenter.default.publisher(for: .openSettingsRequest)) { _ in\n                Task { @MainActor in\n                    // Show dock icon for window focus\n                    NSApp.setActivationPolicy(.regular)\n                    try? await Task.sleep(for: .milliseconds(100))\n                    \n                    // Activate and open\n                    NSApp.activate(ignoringOtherApps: true)\n                    openSettings()\n                    \n                    // 2. No window ordering - After openSettings(), you might need additional window management to ensure\n                    // it comes to front:\n                    try? await Task.sleep(for: .milliseconds(200))\n                    if let settingsWindow = findSettingsWindow() {\n                        settingsWindow.makeKeyAndOrderFront(nil)\n                        settingsWindow.orderFrontRegardless()\n                    }\n                }\n            }\n            .onReceive(NotificationCenter.default.publisher(for: .settingsWindowClosed)) { _ in\n                // Restore menu bar app state when settings closes\n                NSApp.setActivationPolicy(.accessory)\n            }\n    }\n}\n\n// Window identifier for settings\nstatic let settingsWindowIdentifier = &quot;com.apple.SwiftUI.Settings&quot;\n\n/// Finds the settings window using multiple detection methods\nstatic func findSettingsWindow() -&gt; NSWindow? {\n    // Try multiple methods to find the window\n    return NSApp.windows.first { window in\n        // Check by identifier\n        if window.identifier?.rawValue == settingsWindowIdentifier {\n            return true\n        }\n        \n        // Check by title\n        if window.isVisible &amp;&amp; window.styleMask.contains(.titled) &amp;&amp;\n           (window.title.localizedCaseInsensitiveContains(&quot;settings&quot;) ||\n            window.title.localizedCaseInsensitiveContains(&quot;preferences&quot;)) {\n            return true\n        }\n        \n        // Check by content view controller type\n        if let contentVC = window.contentViewController,\n           String(describing: type(of: contentVC)).contains(&quot;Settings&quot;) {\n            return true\n        }\n        \n        return false\n    }\n}\n\n// App structure\n@main\nstruct MenuBarApp: App {\n    var body: some Scene {\n        MenuBarExtra(&quot;My App&quot;, systemImage: &quot;star.fill&quot;) {\n            Button(&quot;Settings...&quot;) {\n                NotificationCenter.default.post(name: .openSettingsRequest, object: nil)\n            }\n            .keyboardShortcut(&quot;,&quot;, modifiers: .command)\n        }\n        \n        // Required Settings scene\n        Settings {\n            SettingsView()\n                .onDisappear {\n                    NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n                }\n        }\n        \n        // Hidden window for context\n        Window(&quot;Hidden&quot;, id: &quot;HiddenWindow&quot;) {\n            HiddenWindowView()\n        }\n        .windowResizability(.contentSize)\n        .defaultSize(width: 1, height: 1)\n    }\n}\n\nextension Notification.Name {\n    static let openSettingsRequest = Notification.Name(&quot;openSettingsRequest&quot;)\n    static let settingsWindowClosed = Notification.Name(&quot;settingsWindowClosed&quot;)\n}\n</span></code></pre>\n<p>The <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/foundation/notificationcenter\"><code>NotificationCenter</code></a> approach decouples the menu action from the window context, allowing the hidden window to handle the actual settings opening.</p>\n<p>For a production-ready implementation with all edge cases (yes, there are some more…) handled, see <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://github.com/amantus-ai/vibetunnel/blob/2a63599ce0b09d139ddc9954f41f2a5840264f9f/mac/VibeTunnel/Utilities/SettingsOpener.swift#L4\">VibeTunnel’s SettingsOpener.swift</a>.</p>\n<h2>Scene Order Matters (Unfortunately)</h2>\n<p>While implementing @steipete’s solution described above, I (@matejkob) discovered another gotcha: <strong>the order of scenes in your App’s body actually affects whether this workaround functions correctly</strong>.</p>\n<p>The working example shown earlier has the scenes in this order:</p>\n<ol>\n<li><code>MenuBarExtra</code></li>\n<li><code>Settings</code></li>\n<li><code>Window</code> (hidden)</li>\n</ol>\n<p>However, I found that if you arrange them like this (which seems equally logical):</p>\n<pre><code class=\"language-swift\"><span class=\"code-snippet\" data-language=\"Swift\">var body: some Scene {\n    MenuBarExtra(&quot;My App&quot;, systemImage: &quot;star.fill&quot;) {\n        ContentView()\n    }\n    .menuBarExtraStyle(.menu)\n    \n    Settings {\n        SettingsView()\n            .onDisappear {\n                NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n            }\n    }\n    \n    // Hidden window for settings context\n    Window(&quot;Hidden&quot;, id: &quot;HiddenWindow&quot;) {\n        HiddenWindowView()\n    }\n    .windowResizability(.contentSize)\n    .defaultSize(width: 100, height: 100)\n}\n</span></code></pre>\n<p>The hidden window won’t be “visible” to the SwiftUI environment system (at least not on macOS Sequoia 15.5), and the <code>openSettings()</code> call will fail silently. However, moving the hidden <code>Window</code> scene <strong>before</strong> the <code>Settings</code> scene makes it work perfectly:</p>\n<pre><code class=\"language-swift\"><span class=\"code-snippet\" data-language=\"Swift\">var body: some Scene {\n    // Hidden window for settings context - MUST come before Settings\n    Window(&quot;Hidden&quot;, id: &quot;HiddenWindow&quot;) {\n        HiddenWindowView()\n    }\n    .windowResizability(.contentSize)\n    .defaultSize(width: 100, height: 100)\n    \n    MenuBarExtra(&quot;My App&quot;, systemImage: &quot;star.fill&quot;) {\n        ContentView()\n    }\n    .menuBarExtraStyle(.menu)\n    \n    Settings {\n        SettingsView()\n            .onDisappear {\n                NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n            }\n    }\n}\n</span></code></pre>\n<p>This suggests that SwiftUI’s scene resolution and environment propagation happens in declaration order, and the <code>@Environment(\\.openSettings)</code> action in the hidden window needs its context established before the <code>Settings</code> scene is processed.</p>\n<p>Whether this is an Xcode build system quirk, a SwiftUI implementation detail, or intended behavior is unclear from Apple’s documentation. What is clear is that <strong>the hidden window must be declared before the Settings scene</strong> for this workaround to function reliably.</p>\n<p>This adds yet another layer of undocumented complexity to what should be a simple operation - not only do we need a hidden window and activation policy juggling, but we also need to carefully order our scene declarations to avoid mysterious failures.</p>\n<h2>Understanding the Workaround</h2>\n<p>The hidden window serves multiple purposes:</p>\n<ul>\n<li>Provides a valid window context for the <code>openSettings</code> environment action</li>\n<li>Allows us to temporarily switch activation policies without visual disruption</li>\n<li>Gives us a place to handle the complex timing orchestration</li>\n</ul>\n<p>The dock icon manipulation (switching between <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/accessory\"><code>.accessory</code></a> and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/regular\"><code>.regular</code></a>) is necessary because macOS only brings windows to the front reliably for apps with dock icons.</p>\n<h2>Fin</h2>\n<p>What should be a one-liner in other frameworks requires careful orchestration in SwiftUI. The combination of <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/swiftui/menubarextra\"><code>MenuBarExtra</code></a>, <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/swiftui/settings\"><code>Settings</code></a> scenes, and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://developer.apple.com/documentation/swiftui/opensettingsaction\"><code>openSettings</code></a> wasn’t designed with the unique constraints of menu bar apps in mind.</p>\n<p>This shouldn’t be so hard. Opening a settings window is one of the most basic operations any app needs to perform. The fact that it requires hidden windows, activation policy juggling, and precise timing delays in 2025 is a testament to how menu bar apps remain second-class citizens in SwiftUI. Until Apple addresses these fundamental issues, we’re stuck with these workarounds.</p>\n",
  "content_markdown": "![](https://steipete.me/assets/img/2025/showing-settings-from-macos-menu-bar-items/hero.png)\n\nOpening a settings window from a macOS menu bar app should be trivial. It’s not. After spending hours debugging, I’m documenting the gotchas to save you the same frustration.\n\n## The Problem\n\nSwiftUI provides [`SettingsLink`](https://developer.apple.com/documentation/swiftui/settingslink) for opening settings:\n\n```swift\nMenuBarExtra(\"Test\", systemImage: \"star.fill\") {\n    SettingsLink {\n        Text(\"Open settings\") \n    }\n}\n```\n\nSimple, right? Except it doesn’t work reliably in [`MenuBarExtra`](https://developer.apple.com/documentation/swiftui/menubarextra). The documentation doesn’t mention this limitation.\n\nAccording to Apple’s documentation, `SettingsLink` should “open the app’s settings scene when activated.” However, this assumes your app is already active and has proper window management context - assumptions that don’t hold for menu bar apps.\n\n## Why It Fails\n\nMenu bar apps operate differently from regular macOS apps:\n\n- **No dock icon by default** - They use [`NSApplication.ActivationPolicy.accessory`](https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/accessory)\n- **Not “active” in the traditional sense** - They don’t appear in the app switcher\n- **Windows may appear behind other apps** - Without proper activation, windows lack focus\n- **No SwiftUI graph loaded** - Settings needs some SwiftUI view initalized and the menu bar uses AppKit under the hood.\n\nThe root issue is that [`NSApplication`](https://developer.apple.com/documentation/appkit/nsapplication) treats menu bar apps as background utilities, not foreground applications. This affects how windows are ordered and receive events.\n\n## The Evolution of Workarounds\n\n### The Old Way\n\nThis is how it used to work. Private API, but widely used and simple:\n\n```swift\nif #available(macOS 13, *) {\n    NSApp.sendAction(Selector((\"showSettingsWindow:\")), to: nil, from: nil)\n} else {\n    // macOS 12 or earlier\n    NSApp.sendAction(Selector((\"showPreferencesWindow:\")), to: nil, from: nil)\n}\n```\n\nThis stopped working in Sonoma (14) with the error: “Please use SettingsLink for opening the Settings scene.” Apple deprecated these selectors in favor of SwiftUI’s scene-based approach, but didn’t account for the unique challenges of menu bar apps.\n\n### The openSettings Environment Action\n\nApple provides an [`openSettings` environment action](https://developer.apple.com/documentation/swiftui/opensettingsaction) for programmatic access (available since macOS 14.0+):\n\n```swift\nstruct MyView: View {\n    @Environment(\\.openSettings) private var openSettings\n\n    var body: some View {\n        Button(\"Open Settings\") {\n            openSettings()\n        }\n    }\n}\n```\n\nThis currently works on macOS 15, but doesn’t work on macOS Tahoe (26). The logic needs an existing SwiftUI render tree, and simply calling the environment variable does nothing if none is found.\n\nThe workaround? As horrible as it sounds, a *hidden window*. Of course, that comes with its own issues, unless you massage the window that it’s really off-screen and ideally also doesn’t react to touches.\n\n## Hide & Seek\n\nNow, this works, however the window will open in the *background*, and no amount of `makeKeyAndOrderFront(nil)` will help. Trust me. I (and [Claude](https://steipete.me/posts/2025/claude-code-is-my-computer/)) tried plenty variations.\n\nThe real reason? macOS doesn’t allow a window to become selected when there’s no Dock icon. And since it’s common to hide the Dock icon for pure Menu Bar apps, that’s a problem.\n\nThe workaround? Show the Dock icon just before calling `openSettings()` and then hiding it again. In a way, this is also convenient for the user as the Icon now represents the “app” - the visible window, and once that closes, we hide the Dock icon again. (via calling `NSApp.setActivationPolicy(.accessory)`). Of course the whole thing requires some delays to really work, so let me present you the final, working solution (I apologize in advance):\n\n## The Working Solution\n\nHere’s the minimal implementation for macOS 14 and higher, using Swift 6:\n\n```swift\n// Hidden window to provide context\nstruct HiddenWindowView: View {\n    @Environment(\\.openSettings) private var openSettings\n    \n    var body: some View {\n        Color.clear\n            .frame(width: 1, height: 1)\n            .onReceive(NotificationCenter.default.publisher(for: .openSettingsRequest)) { _ in\n                Task { @MainActor in\n                    // Show dock icon for window focus\n                    NSApp.setActivationPolicy(.regular)\n                    try? await Task.sleep(for: .milliseconds(100))\n                    \n                    // Activate and open\n                    NSApp.activate(ignoringOtherApps: true)\n                    openSettings()\n                    \n                    // 2. No window ordering - After openSettings(), you might need additional window management to ensure\n                    // it comes to front:\n                    try? await Task.sleep(for: .milliseconds(200))\n                    if let settingsWindow = findSettingsWindow() {\n                        settingsWindow.makeKeyAndOrderFront(nil)\n                        settingsWindow.orderFrontRegardless()\n                    }\n                }\n            }\n            .onReceive(NotificationCenter.default.publisher(for: .settingsWindowClosed)) { _ in\n                // Restore menu bar app state when settings closes\n                NSApp.setActivationPolicy(.accessory)\n            }\n    }\n}\n\n// Window identifier for settings\nstatic let settingsWindowIdentifier = \"com.apple.SwiftUI.Settings\"\n\n/// Finds the settings window using multiple detection methods\nstatic func findSettingsWindow() -> NSWindow? {\n    // Try multiple methods to find the window\n    return NSApp.windows.first { window in\n        // Check by identifier\n        if window.identifier?.rawValue == settingsWindowIdentifier {\n            return true\n        }\n        \n        // Check by title\n        if window.isVisible && window.styleMask.contains(.titled) &&\n           (window.title.localizedCaseInsensitiveContains(\"settings\") ||\n            window.title.localizedCaseInsensitiveContains(\"preferences\")) {\n            return true\n        }\n        \n        // Check by content view controller type\n        if let contentVC = window.contentViewController,\n           String(describing: type(of: contentVC)).contains(\"Settings\") {\n            return true\n        }\n        \n        return false\n    }\n}\n\n// App structure\n@main\nstruct MenuBarApp: App {\n    var body: some Scene {\n        MenuBarExtra(\"My App\", systemImage: \"star.fill\") {\n            Button(\"Settings...\") {\n                NotificationCenter.default.post(name: .openSettingsRequest, object: nil)\n            }\n            .keyboardShortcut(\",\", modifiers: .command)\n        }\n        \n        // Required Settings scene\n        Settings {\n            SettingsView()\n                .onDisappear {\n                    NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n                }\n        }\n        \n        // Hidden window for context\n        Window(\"Hidden\", id: \"HiddenWindow\") {\n            HiddenWindowView()\n        }\n        .windowResizability(.contentSize)\n        .defaultSize(width: 1, height: 1)\n    }\n}\n\nextension Notification.Name {\n    static let openSettingsRequest = Notification.Name(\"openSettingsRequest\")\n    static let settingsWindowClosed = Notification.Name(\"settingsWindowClosed\")\n}\n```\n\nThe [`NotificationCenter`](https://developer.apple.com/documentation/foundation/notificationcenter) approach decouples the menu action from the window context, allowing the hidden window to handle the actual settings opening.\n\nFor a production-ready implementation with all edge cases (yes, there are some more…) handled, see [VibeTunnel’s SettingsOpener.swift](https://github.com/amantus-ai/vibetunnel/blob/2a63599ce0b09d139ddc9954f41f2a5840264f9f/mac/VibeTunnel/Utilities/SettingsOpener.swift#L4).\n\n## Scene Order Matters (Unfortunately)\n\nWhile implementing @steipete’s solution described above, I (@matejkob) discovered another gotcha: **the order of scenes in your App’s body actually affects whether this workaround functions correctly**.\n\nThe working example shown earlier has the scenes in this order:\n\n1. `MenuBarExtra`\n2. `Settings`\n3. `Window` (hidden)\n\nHowever, I found that if you arrange them like this (which seems equally logical):\n\n```swift\nvar body: some Scene {\n    MenuBarExtra(\"My App\", systemImage: \"star.fill\") {\n        ContentView()\n    }\n    .menuBarExtraStyle(.menu)\n    \n    Settings {\n        SettingsView()\n            .onDisappear {\n                NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n            }\n    }\n    \n    // Hidden window for settings context\n    Window(\"Hidden\", id: \"HiddenWindow\") {\n        HiddenWindowView()\n    }\n    .windowResizability(.contentSize)\n    .defaultSize(width: 100, height: 100)\n}\n```\n\nThe hidden window won’t be “visible” to the SwiftUI environment system (at least not on macOS Sequoia 15.5), and the `openSettings()` call will fail silently. However, moving the hidden `Window` scene **before** the `Settings` scene makes it work perfectly:\n\n```swift\nvar body: some Scene {\n    // Hidden window for settings context - MUST come before Settings\n    Window(\"Hidden\", id: \"HiddenWindow\") {\n        HiddenWindowView()\n    }\n    .windowResizability(.contentSize)\n    .defaultSize(width: 100, height: 100)\n    \n    MenuBarExtra(\"My App\", systemImage: \"star.fill\") {\n        ContentView()\n    }\n    .menuBarExtraStyle(.menu)\n    \n    Settings {\n        SettingsView()\n            .onDisappear {\n                NotificationCenter.default.post(name: .settingsWindowClosed, object: nil)\n            }\n    }\n}\n```\n\nThis suggests that SwiftUI’s scene resolution and environment propagation happens in declaration order, and the `@Environment(\\.openSettings)` action in the hidden window needs its context established before the `Settings` scene is processed.\n\nWhether this is an Xcode build system quirk, a SwiftUI implementation detail, or intended behavior is unclear from Apple’s documentation. What is clear is that **the hidden window must be declared before the Settings scene** for this workaround to function reliably.\n\nThis adds yet another layer of undocumented complexity to what should be a simple operation - not only do we need a hidden window and activation policy juggling, but we also need to carefully order our scene declarations to avoid mysterious failures.\n\n## Understanding the Workaround\n\nThe hidden window serves multiple purposes:\n\n- Provides a valid window context for the `openSettings` environment action\n- Allows us to temporarily switch activation policies without visual disruption\n- Gives us a place to handle the complex timing orchestration\n\nThe dock icon manipulation (switching between [`.accessory`](https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/accessory) and [`.regular`](https://developer.apple.com/documentation/appkit/nsapplication/activationpolicy/regular)) is necessary because macOS only brings windows to the front reliably for apps with dock icons.\n\n## Fin\n\nWhat should be a one-liner in other frameworks requires careful orchestration in SwiftUI. The combination of [`MenuBarExtra`](https://developer.apple.com/documentation/swiftui/menubarextra), [`Settings`](https://developer.apple.com/documentation/swiftui/settings) scenes, and [`openSettings`](https://developer.apple.com/documentation/swiftui/opensettingsaction) wasn’t designed with the unique constraints of menu bar apps in mind.\n\nThis shouldn’t be so hard. Opening a settings window is one of the most basic operations any app needs to perform. The fact that it requires hidden windows, activation policy juggling, and precise timing delays in 2025 is a testament to how menu bar apps remain second-class citizens in SwiftUI. Until Apple addresses these fundamental issues, we’re stuck with these workarounds.\n",
  "dateCreated": "2026-09-27T19:29:15.293681927+00:00",
  "date_created": "2025-06-17T11:00:00+00:00",
  "date_modified": "2025-06-17T11:00:00+00:00",
  "date_published": "2025-06-17T11:00:00+00:00",
  "description": "Why something as simple as showing a settings dialog from a macOS menu bar app took me 5 hours to figure out, and requires 50 lines of code for what should be a one-liner.",
  "external_url": "https://steipete.me/posts/2025/showing-settings-from-macos-menu-bar-items/",
  "git": {
    "commit": "ebb0941c5c06a2ab2377342d607535262a3e969e",
    "path": "items/steipete-me/2025/06/2025-06-17-showing-settings-from-macos-menu-bar-items-a-5-hour-journey.md",
    "permalink": "https://github.com/aymericbeaumet/aggr-instance/blob/ebb0941c5c06a2ab2377342d607535262a3e969e/items/steipete-me/2025/06/2025-06-17-showing-settings-from-macos-menu-bar-items-a-5-hour-journey.md"
  },
  "id": "urn:aggr:item:88a65b1236a96ab039b1e9155a3ba0a7e927ad2a",
  "isBasedOn": {
    "@id": "https://steipete.me/posts/2025/showing-settings-from-macos-menu-bar-items/"
  },
  "mainEntity": {
    "@id": "https://steipete.me/posts/2025/showing-settings-from-macos-menu-bar-items/",
    "@type": "CreativeWork",
    "archivedAt": {
      "@id": "https://aggr.aymericbeaumet.com/items/steipete-me/2025-06-17-showing-settings-from-macos-menu-bar-items-a-5-hour-journey/#webpage"
    },
    "dateModified": "2025-06-17T11:00:00+00:00",
    "datePublished": "2025-06-17T11:00:00+00:00",
    "headline": "Showing Settings from macOS Menu Bar Items: A 5-Hour Journey",
    "timeRequired": "PT6M",
    "url": "https://steipete.me/posts/2025/showing-settings-from-macos-menu-bar-items/",
    "wordCount": 1244
  },
  "name": "Archived snapshot: Showing Settings from macOS Menu Bar Items: A 5-Hour Journey",
  "preview": null,
  "reading_minutes": 6,
  "replicated_at": null,
  "source": "steipete-me",
  "source_name": "Peter Steinberger",
  "summary": "Why something as simple as showing a settings dialog from a macOS menu bar app took me 5 hours to figure out, and requires 50 lines of code for what should be a one-liner.",
  "tags": [],
  "temporalCoverage": "2026-09-27T19:29:15.293681927+00:00",
  "title": "Showing Settings from macOS Menu Bar Items: A 5-Hour Journey",
  "url": "https://aggr.aymericbeaumet.com/items/steipete-me/2025-06-17-showing-settings-from-macos-menu-bar-items-a-5-hour-journey/",
  "word_count": 1244
}