Frontmost window

  • I'm trying to determine which window in my app is frontmost.
    It might not necessarily be key if my app isn't in the foreground.
    I found a technique that works, but is there a simpler API that I'm missing?

        NSWindow * windowInFront = [NSApp
    makeWindowsPerform:@selector(myWindowIsInFront:) inOrder:YES];

    @implementation NSWindow (MyAdditions)
    - (id) myWindowIsInFront:(id)sender {
        return (id)YES;
    }
    @end
  • To clarify a bit:
    NSApp -windows in the docs does not seem to guarantee any ordering at all.
    NSApp -orderedWindows seems close, but is documented to exclude some
    windows from the list, like panels. I'd want to include all regular windows.

    John Stiles wrote:
    > I'm trying to determine which window in my app is frontmost.
    > It might not necessarily be key if my app isn't in the foreground.
    > I found a technique that works, but is there a simpler API that I'm
    > missing?
    >
    >
    > NSWindow * windowInFront = [NSApp
    > makeWindowsPerform:@selector(myWindowIsInFront:) inOrder:YES];
    >
    >
    > @implementation NSWindow (MyAdditions)
    > - (id) myWindowIsInFront:(id)sender {
    > return (id)YES;
    > }
    > @end
  • On Nov 16, 2007, at 4:50 PM, John Stiles wrote:

    > I'm trying to determine which window in my app is frontmost.
    > It might not necessarily be key if my app isn't in the foreground.
    > I found a technique that works, but is there a simpler API that I'm
    > missing?

    You can use the AppKit function "NSWindowList" for this purpose. Note
    that it only includes onscreen windows, so miniaturized windows will,
    as an example, be excluded.

    Cheers,

    j o a r
  • It looks like that lists /all/ windows, not just /my /windows.

    They have a "NSWindowListForContext" but the docs discourage its use
    because it may be removed in a future release. They also don't explain
    how to get a context number for my own app (though I could probably
    figure that out if the API weren't deprecated).

    j o a r wrote:
    >
    > On Nov 16, 2007, at 4:50 PM, John Stiles wrote:
    >
    >> I'm trying to determine which window in my app is frontmost.
    >> It might not necessarily be key if my app isn't in the foreground.
    >> I found a technique that works, but is there a simpler API that I'm
    >> missing?
    >
    >
    > You can use the AppKit function "NSWindowList" for this purpose. Note
    > that it only includes onscreen windows, so miniaturized windows will,
    > as an example, be excluded.
    >
    > Cheers,
    >
    > j o a r
    >
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today