Last active
August 7, 2024 10:12
-
-
Save othyn/98f35abf988bdcfb6a118b8573d46b3b to your computer and use it in GitHub Desktop.
How to disable default menu bar items in Swift / SwiftUI for macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// App.swift | |
// | |
// Created by Ben Tindall on 30/03/2022. | |
// | |
import Foundation | |
import SwiftUI | |
import Cocoa | |
final class AppDelegate: NSObject, NSApplicationDelegate { | |
func applicationWillUpdate(_ notification: Notification) { | |
if let menu = NSApplication.shared.mainMenu { | |
if let file = menu.items.first(where: { $0.title == "File"}) { | |
menu.removeItem(file); | |
} | |
if let edit = menu.items.first(where: { $0.title == "Edit"}) { | |
menu.removeItem(edit); | |
} | |
if let window = menu.items.first(where: { $0.title == "Window"}) { | |
menu.removeItem(window); | |
} | |
if let view = menu.items.first(where: { $0.title == "View"}) { | |
menu.removeItem(view); | |
} | |
if let help = menu.items.first(where: { $0.title == "Help"}) { | |
menu.removeItem(help); | |
} | |
} | |
} | |
} | |
@main | |
struct AutoClickerApp: App { | |
@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate | |
var body: some Scene { | |
WindowGroup { | |
EmptyView() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked like a charm. Thanks!