NSWindow returns nil (NSApplication)

It also can return nil if the main app window is not active on macOS. For instance I ran into this when I was making a drag and drop file uploader. If the window was not in the front (on the operating system) it would return nil. The following line of code will activate your app (bring to front)

NSApp.activateIgnoringOtherApps(true)

I also needed a timer to delay my call of mainWindow in my case.


You could do this instead:

   func applicationDidBecomeActive(notification: NSNotification) {

        NSApplication.sharedApplication().mainWindow?.movable = false
    }