IGHOR
IGHOR

Reputation: 710

How to get Position, Width and Height of Mac OS X Dock? Cocoa/Carbon/C++/Qt

I'm trying to get position and width of Mac OS X Dock in my C++/Qt app. But I can only find ways to get Available space of Desktop, it means I can get Dock height, but not width.

Is there ways to get Dock position and width using native OS API?

Upvotes: 6

Views: 3922

Answers (3)

Andrew
Andrew

Reputation: 7720

To expand upon MacAndor's answer, you can infer the dock position by comparing the -[NSScreen visibleFrame] (which excludes the space occupied by the dock and the menu bar) with the -[NSScreen frame] which encompasses the entire screen width and height.

The example code below is dependent on the screen the window resides on. This code can be adapted to work with multiple displays by enumerating through all screens instead of using the window's screen.

// Infer the dock position (left, bottom, right)
NSScreen *screen = [self.window screen];    
NSRect visibleFrame = [screen visibleFrame];
NSRect screenFrame = screen.frame;

if (visibleFrame.origin.x > screenFrame.origin.x) {
    NSLog(@"Dock is positioned on the LEFT");
} else if (visibleFrame.origin.y > screenFrame.origin.y) {
    NSLog(@"Dock is positioned on the BOTTOM");
} else if (visibleFrame.size.width < screenFrame.size.width) {
    NSLog(@"Dock is positioned on the RIGHT");
} else {
    NSLog(@"Dock is HIDDEN");
}

Upvotes: 5

MacAndor
MacAndor

Reputation: 205

This might help in a hack-free solution, NSScreen provides a method (visibleframe) which subtracts the menu and the Dock from the screen size. The frame method contains both.

[NSStatusBar system​Status​Bar].thickness will return the height of the Menu bar.

https://developer.apple.com/reference/appkit/nsscreen/1388369-visibleframe?language=objc

Upvotes: 11

dtech
dtech

Reputation: 49289

Doesn't see like such a thing is possible, not even from the apple APIs, much less from Qt.

The only solution that comes to mind, a rather crude one, is to take a screenshot and use some basic image recognition to find the position and dimensions of the dock.

Upvotes: 0

Related Questions