1

Is there a SwiftUI idiomatic way to selectively remove the defualt (File, Edit, and View) menus from the menu bar in a macOS app, while keeping other menus like the AppName menu intact? The app I’m building is a simple utility, so Edit and View menus are not relevant in this context.

I’m aware of the new .commandsRemoved(), available since macOS 13, but that apparently removes all menus.

I’m looking for a pure SwiftUI solution. Any guidance and help would be appreciated.

4
  • Does this answer your question? stackoverflow.com/questions/65209295/… Commented Oct 30, 2023 at 17:00
  • @jnpdx No. The solution is quite inelegant since my app is localized in numerous languages. I would need to find the menu names for each language and then match the strings. Anyway, thanks for the suggestion. Commented Oct 30, 2023 at 17:15
  • @jnpdx UIMenuBuilder isn’t available on macOS. I’m not using Mac Catalyst. Commented Oct 30, 2023 at 17:48
  • To anyone interested, please read: developer.apple.com/forums/thread/… Commented Nov 1, 2023 at 21:47

2 Answers 2

0

You could try removing all commands using .commandsRemoved(), then adding back only the ones you want. You can also show/hide the particular command using a condition.

 @main
 struct TestMacApp: App {
     @State var showView = false
     
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
         .commandsRemoved()
         .commandsReplaced {
             CommandGroup(replacing: .help, addition: {})
             if showView { CommandGroup(replacing: .textEditing, addition: {}) }
             // ...
         }
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I should have mentioned that was one of the first things I tried. However, this method does not work. AppNamemenu remains empty after you bring back .appInfo, .appVisibility, .appTermination, etc.
0

It's been a little while since asked, but I came across this myself. My only way to get it to work is similar to workingdog support Ukraine's answer, but includes adding a Divider(). Curiously, an EmptyView() wouldn't work.

Bonus: doing this also allowed me to use the (cmd+n) keyboard shortcut for my own app's purposes (not creating a new window, but a new item).

Example:

@main
struct TestMacApp: App {
     @State var showView = false
     
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
         .commandsRemoved()
         .commandsReplaced {
            CommandGroup(replacing: .newItem) {
                Divider()
            }
         }
     }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.