Skip to content

Instantly share code, notes, and snippets.

@tonyarnold
Created June 8, 2012 02:52
Show Gist options
  • Select an option

  • Save tonyarnold/2893241 to your computer and use it in GitHub Desktop.

Select an option

Save tonyarnold/2893241 to your computer and use it in GitHub Desktop.
Retrieve the frame of the OS X desktop as a single NSRect
//
// NSScreen+CBAdditions.h
// Hyperspaces
//
// Created by Tony Arnold on 2/11/10.
// Copyright 2010 The CocoaBots. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSScreen (CBAdditions)
+ (NSRect)desktopFrame;
@end
//
// NSScreen+CBAdditions.m
// Hyperspaces
//
// Created by Tony Arnold on 2/11/10.
// Copyright 2010 The CocoaBots. All rights reserved.
//
#import "NSScreen+CBAdditions.h"
@implementation NSScreen (CBAdditions)
+ (NSRect)desktopFrame {
NSArray *screens = [NSScreen screens];
NSRect frame = NSZeroRect;
NSScreen *screen = nil;
if (!screens) {
return frame;
}
// Menubar is the 0th screen (according to the NSScreen docs)
NSEnumerator *e = [screens objectEnumerator];
frame = [[screens objectAtIndex:0] frame];
while ((screen = [e nextObject])) {
// Union the entirety of the frame (not just the visible area)
frame = NSUnionRect(frame, [screen frame]);
}
return frame;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment