2
- (void) GetClientRect 

    NSArray *screenArray = [NSScreen screens];
    NSScreen *mainScreen = [NSScreen mainScreen];
    unsigned screenCount = [screenArray count];
    unsigned index  = 0;

    for (index; index < screenCount; index++)
    {
        NSScreen *screen = [screenArray objectAtIndex: index];
        NSRect screenRect = [screen visibleFrame];
        NSString *mString = ((mainScreen == screen) ? @"Main" : @"not-main");

        NSLog(@"Screen #%d (%@) Frame: %@", index, mString, NSStringFromRect(screenRect));
    }
}

Above method is use to get frame for mainscreen. But i want method which return Screen size of NSWindow whose kCGWindowNumber have been passed. Any idea ?!

V.D
  • 403
  • 1
  • 6
  • 22

1 Answers1

2

Why are you working with a CGWindowID (which is what I assume you meant by kCGWindowNumber)? That's a very unnatural thing to do in most circumstances.

Anyway, you can call CGWindowListCopyWindowInfo(0, theWindowID) and examine the dictionary in the first element of the returned array. Use the key kCGWindowBounds to get a dictionary representation of the bounds rectangle and then CGRectMakeWithDictionaryRepresentation() to convert that to a CGRect.


Are you looking for how to convert the NSWindow -frame, which is in Cocoa's coordinate system, to the Core Graphics coordinate system? The Cocoa coordinate system has its origin at the bottom-left of the primary display, with Y increasing in the up direction. The Core Graphics coordinate system has its origin at the top-left of the primary display, with Y increasing in the down direction.

So, the conversion looks like this:

NSWindow* window = /* ... comes from wherever ... */;
NSRect frame = window.frame;
frame.origin.y = NSMaxY([NSScreen.screens.firstObject frame]) - NSMaxY(frame);
CGRect cgbounds = NSRectToCGRect(frame);
Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Hi @Ken the reason I need to get from `NSWindow` to `CGWindowID` is because I am trying to move a newly created window, to another workspace (Firefox creates the new window and gives me the `NSWindow` to it) is it possible to get `kCGWindowNumber` from `NSWindow`? I am using this method to move window to another workspace - http://stackoverflow.com/a/2368945/1828637 – Noitidart Mar 18 '16 at 05:04
  • I thought to use [`[NSWindow windowNumber]`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/index.html#//apple_ref/occ/instp/NSWindow/windowNumber) but the docs warn its not the same number. – Noitidart Mar 18 '16 at 05:13
  • 1
    There is no documented way to get a `CGWindowID` from an `NSWindow`. It's not clear that the `-windowNumber` warning is really about `CGWindowID` or some other number used by the window server. Even if it is, it might mean that `-windowNumber` is not *always* equal to the window's `CGWindowID` because it's negative for off-screen windows. In practice, plenty of people have relied on the fact that, for on-screen windows, `-windowNumber` is the same as the `CGWindowID`. – Ken Thomases Mar 18 '16 at 07:28
  • 1
    If you don't want to rely on that assumption, your code can verify it by getting the window info for a window whose `CGWindowID` is the `NSWindow`'s `-windowNumber`, using `CGWindowListCopyWindowInfo()`, and then compare all of the properties to the `NSWindow`'s. If that fails to find a window or the properties don't match, you can get the info for all windows using that same function, search for the one whose properties all match, and take the `kCGWindowNumber` from that. This is prone to false positives if there are two identical windows in your target process. – Ken Thomases Mar 18 '16 at 07:37
  • 1
    I've updated my answer with instructions for how to convert from `NSWindow`'s `-frame` to the Core Graphics coordinate system, which will be necessary to compare to the bounds in the window info dictionaries under the `kCGWindowBounds` key. – Ken Thomases Mar 18 '16 at 07:45
  • I did a test. I listed all the windows and their titles and their id with `CGWindowListCopyWindowInfo`, then I did `windowNumber` on the `NSWindow` firefox gave me. They matched, but I don't know if I can count it, due to that warning on the docs page. Your not on "on screen windows the `windowNumber` is the `CGWindowID` for fact" helps me incredibly! Thank you very much! – Noitidart Mar 18 '16 at 09:00
  • By the way I don't know if you know but I credit you in my addon description page as top of list contributor for my work :D https://addons.mozilla.org/en-US/firefox/addon/nativeshot/ - http://i.imgur.com/2HNOrTt.png :) thanks so much for all your help to me and everyone :) – Noitidart Mar 18 '16 at 09:03
  • 1
    That's very gracious of you. You're welcome. I'm glad I could help. – Ken Thomases Mar 18 '16 at 10:07
  • Thanks for the kind words =) – Noitidart Mar 18 '16 at 12:55