Frontmost window of the current application that passes a predicate check After resigning active state (going to the background), the mainWindow property of the main window in the app returns false.

Nevertheless, I need a to find that window even when my app is not active, so I came up with the following code, which works:

public let App = NSApplication.sharedApplication() // just some sugar

public extension NSApplication {

    public func frontmostWindow(_ accept: (NSWindow -&gt; Bool)? = nil) -&gt; NSWindow? {
        for windowNumber in NSWindow.windowNumbersWithOptions(0) as? [Int] ?? [] {
            if let window = windowWithWindowNumber(windowNumber) {
                if let accept = accept {
                    if accept(window) {
                        return window
                    }
                } else {
                    return window
                }
            }
        }
        return nil
    }
}


With this, I can now get the first window of my app in the current ordering that passes the provided predicate check (accept: NSWindow -&gt; Bool). This is because I normally would not be interested in the menu bar or the status item, which all have associated windows that belong to my app (which is what I would get if I do not pass any predicate). As an example, say I have some:

class PimpedUpWindow: NSWindow, ExtraTranslucent {}


the frontmost instance of which I wish to find after the app launches and is no longer the active app. Using the extension above I can:

App.frontmostWindow { $0 is PimpedUpWindow } 


or

App.frontmostWindow { $0 is ExtraTranslucent } 


Of course, I can provide any other predicate of type NSWindow -&gt; Bool, but I figure I will often enough want to just check the type or protocol conformance. So I might as well add two convenience methods for just those occasions:

public extension NSApplication {

    public func frontmost &lt;T&gt; (windowOfType: T.Type) -&gt; T? {
        return frontmostWindow { $0 is T } as? T
    }

    public func frontmostWindow &lt;P&gt; (conformingTo: P.Type) -&gt; NSWindow? {
        return frontmostWindow { $0 is P }
    }
}


Now, I can simply:

App.frontmost(PimpedUpWindow)


Which actually returns a PimpedUpWindow?, and:

App.frontmostWindow(ExtraTranslucent)


which returns an NSWindow?.

All ideas, criticisms, and comments very welcome!
There is a way of improving the code as desired in that it is possible to constrain the type T to T: NSWindow. Upon introducing this change, however, the function will indeed return the first NSWindow in the current ordering, whether or not it is of subtype T (too check this you must construct a test that can actually fail, i.e. by having an NSWindow of a another type above the window you are looking for - e.g. by adding a status item to your app). There is a workaround, however, as described in Optional binding succeeds if it shouldn't. Apart from the solution I stumbled upon in the question (of not constraining T), there is a much preferable option discovered by @vacawama in the answer to the linked question. The workaround is to generalise the variable to Any before type checking, e.g. as follows:

public func frontmost &lt;T: NSWindow&gt; (windowOfType: T.Type) -&gt; T? {
    return frontmostWindow { $0 as Any is T } as? T
}


This improves the api considerably.