0

So by default I am seeing this options for my app in macOS SwiftUI:

I would like to remove File, edit, view, window and just keeping help. How can I remove them?


I was trying to remove File, edit ... and I found this codes in internet, but not sure how I can make them works:

  @main
struct My_TestApp: App {
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    final class AppDelegate: NSObject, NSApplicationDelegate {
        
        func applicationWillUpdate(_ notification: Notification) {
            if let menu = NSApplication.shared.mainMenu {
                menu.items.removeFirst{ $0.title == "File" }
                menu.items.removeFirst{ $0.title == "Edit" }
                menu.items.removeFirst{ $0.title == "View" }
                menu.items.removeFirst{ $0.title == "Window" }
            }
        }
        
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

The Xcode error is:

Trailing closure passed to parameter of type 'Int' that does not accept a closure

code13de
  • 21
  • 5
  • 1
    Why do you want to do this? It'll create a very very poor user experience. – Claus Jørgensen Jun 03 '22 at 13:05
  • Have you taken a look at [this question](https://stackoverflow.com/questions/65552584/swiftui-changing-default-command-menus-on-macos)? – Mr Developer Jun 03 '22 at 17:28
  • @ClausJørgensen: For the app I am working on, just help is more than enough. It is very simple app and I want to be as simple as possible. – code13de Jun 03 '22 at 21:34
  • @MrDeveloper: thanks, I think that links adds new items, I want remove items. – code13de Jun 03 '22 at 21:35
  • @code13de you don't seem to understand how users usually quit a app on MacOS. The CMD+Q uses the shortcuts defined in the File menu. If you remove it they can't actually close the app. And without "Window" they can't restore the window if they close it (using the red dot). So you don't seem to understand how apps are written on MacOS at all. – Claus Jørgensen Jun 04 '22 at 16:26
  • @ClausJørgensen: It is correct, that you said I do not understand, I believe you understand it. I am a hungry foolish man. – code13de Jun 04 '22 at 22:18

1 Answers1

0

It is named Main Menu, just in case.

A possible approach is to filter created menu after launch. So create app delegate adapter and on did finish, do the following:

func applicationDidFinishLaunching(_ notification: Notification) {
    guard let mainMenu = NSApp.mainMenu else { return }
    let newMenu = NSMenu()
    if let appMenu = mainMenu.items.first {
        mainMenu.removeItem(appMenu) // cannot be in both, so remove from previous
        newMenu.addItem(appMenu)
    }
    if let helpMenu = NSApp.mainMenu?.items.last {
        mainMenu.removeItem(helpMenu)
        newMenu.addItem(helpMenu)
    }
    NSApp.mainMenu = newMenu   // << here !!
}

Tested with Xcode 13.4 / macOS 12.4

backup

Asperi
  • 173,274
  • 14
  • 284
  • 455
  • Not sure how your answer would works or solve my issue. ☹️ – code13de Jun 03 '22 at 23:02
  • Did you try it with set up adapter, https://stackoverflow.com/a/62538373/12299030 – Asperi Jun 04 '22 at 00:54
  • Not sure how your answer can be used, that link did not helped me. May your answer is clear for you because you answered, but for me is not clear. I noticed you are referencing a link which use UIKit in macOS! UIKIt is not available for macOS. – code13de Jun 04 '22 at 11:22
  • 1
    Oh, c`mon... https://stackoverflow.com/questions/71291654/swiftui-appdelegate-on-macos – Asperi Jun 04 '22 at 11:54