Maciek
Maciek

Reputation: 211

Document based application - how to open recent documents merged in one window

When my app (document based) starts I open all recent documents. All documents starts at separate windows. My goal is to open all in one window in tabs.

Upvotes: 0

Views: 386

Answers (1)

Joe Pagliaro
Joe Pagliaro

Reputation: 346

In Swift, try this: In your app delegate implement applicationDidFinishLaunching by calling 'window.mergeAllWindows' where 'window' is the window of the first window controller of the first document. Note that mergeAllWindows is available in Mac OS 10.12.

'DispatchQueue.main.asyncAfter' was used to ensure windows have been restored by the time mergeAllWindows is called, you may prefer a better way to ensure all windows have been restored.

func applicationDidFinishLaunching(_ aNotification: Notification) {

    let dc = NSDocumentController.shared()

    // …

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) { () -> Void in

        if dc.documents.count > 0 {
            let doc = dc.documents[0]

            let wcs = doc.windowControllers

            guard let window = wcs[0].window else { return }

            if #available(OSX 10.12, *) {
                window.mergeAllWindows(self)
            } else {
                // Fallback on earlier versions
            }
        }
    }        
}

Upvotes: 1

Related Questions