NSDocumentController not adding "Open Recent" menu

I have a SwiftUI based app. For lots of reasons I was forced to use NSDocument instead of using DocumentGroup.

I configure the main menu in my AppDelegate.

It has the following code:

        let fileMenuItem = NSMenuItem()
        let fileMenu = NSMenu(title: "File")
        fileMenu.addItem(withTitle: "New", action: #selector(NSDocumentController.newDocument(_:)), keyEquivalent: "n")
        fileMenu.addItem(withTitle: "Open...", action: #selector(NSDocumentController.openDocument(_:)), keyEquivalent: "o")

The New and Open work as expected.

It is my understanding that the NSDocumentController should automatically add the "Open Recent" menu when it sees the Open action based on the NSDocumentController.

It is not appearing.

When I print the state of the recent documents using

print("recent documents \(NSDocumentController.shared.recentDocumentURLs), maximum \(NSDocumentController.shared.maximumRecentDocumentCount)")

I see the recent document urls and a count of 10.

What can I do to make the menu appear?

Thanks for the help.

Interestingly, the recently opened documents are appearing in the dock icon when I right-click (automatically) - and I can successfully open them from there, so I am baffled why the Open Recent menu is not working.

edit: I notice though that the recent documents in the dock icon do not update - when I open a document and close it. I see the following error in the console:

Insert failed for list identifier com.apple.LSSharedFileList.RecentDocuments Error: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (Restricted by sandbox) UserInfo={NSDebugDescription=Restricted by sandbox}

but I don't see any sandbox entitlements that control this.

I have a done a bit more diagnosis. The "Insert failed..." error in the previous message is caused when it tries to add a recent document that is mirrored in iCloud.

If I use a file from a directory not mapped to iCloud I do not get the error, and the file appears in the dock's recent items.

I still cannot get the File "Open Recent" menu to populate.

I am using XCode 16.2 on OSX 15.3.2

If I manually create the Open Recent menu and add the item for the 'Clear Menu' it properly reflects the state of the list (which isn't appearing). If I clear it, the 'Clear Menu' button becomes disabled automatically - so the NSDocumentController is correctly finding the menu items, but it isn't populating the list.

NSDocumentController not adding "Open Recent" menu
 
 
Q