Up: AppKit
Disclaimer: I am an AppKit newbie. Take all of this with a grain of salt.
In an AppKit document-based application, it can be helpful to show the “open” panel on startup, with the option of creating a new document from that open panel.
To do so, first override applicationShouldOpenUntitledFile() in NSApplicationDelegate to return false (otherwise it’ll open an untitled file on startup):
func applicationShouldOpenUntitledFile(
_ app: NSApplication
) -> Bool {
return false
}
To show the open panel, override applicationShouldHandleReopen() to trigger the openDocument action (passing in nil as the irrelevant sender) if there are no open windows (including panels like the open panel):
func applicationShouldHandleReopen(
_ app: NSApplication,
hasVisibleWindows: Bool
) -> Bool {
if app.windows.count(where: { $0.isVisible }) > 0 {
return true
}
NSDocumentController.shared.openDocument(nil)
return false
}
Do not override applicationDidFinishLaunching() to show the open panel, or you’ll get two open panels.