UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to replace "New" document menu or implement a custom Open Recent menu?

Forums > SwiftUI

I have a MacOS Swiftui Document based App. For good reasons I won't go into, I want to replace the "New" action from the main menu bar with my own action - let's call it "New Project". I have that button and its action working and installed after the standard New/Open/OpenRecent File Menu items. But leaving the standard New button there isn't correct as it tries to create a document without all the extra stuff my App really needs. I'm looking for advice on this. I've considered:

  • Tricking the system into using my custom process when New is clicked. Some sort of delegation. I can't find a method to do that.
  • Somehow disabling the New menu item. No clue how to do that.
  • Replacing the New menu button with my custom version. This doesn't seem possible because CommandGroup(replacing:) only offers the option .newItem, which includes New/Open/OpenRecent. In other words, you can't just replace New without replacing Open and OpenRecent. Is there some way to just replace New? I haven't found one.
  • Replacing all three of New/Open/OpenRecent.
    • I have the "new New" ready to go
    • I know how to make my own Open button with NSDocumentController.shared.beginOpenPanel() which returns a document URL which can be fed into NSDocumentController.shared.openDocument(). This works.
    • I can partially implement the OpenRecents menu. This is where I get stuck. I can fetch the list of recent URLs when the app first starts up. I can manually update that data and get it into the menu. The problem is how to get that list to update automatically when, for example, a new document is opened. I can't make a @State dependency on that external source of data. I somehow need to get notified when NSDocumentController's list gets changed. I considered having this data get periodically refreshed, but that's not very Swiftian. How can I do this?

Any suggestions appreciated. Tom Coates :::/

   

To customize "New" action in macOS SwiftUI Document app, consider options like menu bar customization, disabling standard "New" item, or replacing it. Direct replacement without affecting other standard items may be tricky due to SwiftUI limitations. Explore alternative approaches or consult SwiftUI documentation for solutions.

   

John- Thanks for considering my questions. I know you meant well but, as I said in my original post, I've already investigated menu bar customizations, disabling the New item, replacing New, etc. I'm well aware that replacing New might be tricky. I've already spent a frustrating number of hours exploring the documentation and alternative approaches. That's why I asked here, in hope that someone had a better solution. If this was easy, it would be done and I'd be happy to post how. It could be that there is NO good solution to this problem.

To me, the ultimate question is why Apple, in its wisdom, decided that you could only replace all three menu items instead of providing fine grained control over the them. This question is mentioned in several other sites I've looked at. No amount of consulting the sketchy documentation is going to answer that. Meanwhile, I'll sign up as an Apple Developer and post this question on their developer forum.

Cheers Tom Coates :::/

   

You need to integrate AppKit, and it's kludgy -- see the discussion at:

stackoverflow.com/questions/71309874/hiding-edit-menu-of-a-swiftui-macos-app

p.s. Be alert to chat bots on this forum such as johnwhite

   

Bob- Thanks for the pointer and the chatbot warning. I had seen the stackoverflow posting and it did seem kludgy. But it may be the best i can hope for. I will try something from there and report back. Tom :::/

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.