6

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?

IGHOR
  • 691
  • 6
  • 22
  • Can you explain why you need the width of the Dock? When you resize manually a window you cannot go below the Dock. This is possible only when you move a window to the bottom, and it's quite useless. – MBach Mar 15 '16 at 21:44
  • Might get a clue from this link http://www.cocoabuilder.com/archive/cocoa/281435-dock-position-and-size.html – Dendi Suhubdy Mar 16 '16 at 23:44
  • Matthieu, I want to make background window/widget that is linked to Dock size. – IGHOR Mar 17 '16 at 00:35
  • Dendi Suhubdy, yes it maybe partial solution, how to get Dock icons count than? – IGHOR Mar 17 '16 at 00:36
  • 1
    You could get the number of items in it by reading `~/Library/Preferences/com.apple.dock.plist `, `persistent-apps` and `persistent-others`. Then calculate some estimation of the Dock's width using its height (cf other answers). – tmlen Feb 12 '19 at 19:19
  • tmlen, this is nice idea, and I'll use it. But what about new windows that's not pinned to dock? – IGHOR Feb 12 '19 at 23:30

3 Answers3

11

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

MacAndor
  • 205
  • 2
  • 6
  • 1
    Thanks for answer. There is no problems about height, I can't get Width. – IGHOR Mar 20 '17 at 21:17
  • Well, the only API I know which you can use for that is Apple's Accessibility API, though it depends on your use case whether I would recommend it or not: https://developer.apple.com/reference/applicationservices/axuielement.h?language=objc – MacAndor Mar 21 '17 at 10:07
5

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");
}
Andrew
  • 7,630
  • 3
  • 42
  • 51
  • 1
    Thanks for the detailed answer. But it is not what I have ask. I need Width for Dock while it on the bottom. And Height while it on left or right. Also getting new size if any dock item removed/added. – IGHOR Aug 01 '18 at 11:56
  • @Andrew +1 for the insight. @IGHOR, the width and heights you need are right there in the if statement: `dockLeft = visibleFrame.origin.x - screenFrame.origin.x;` and `dockHeight = visibleFrame.origin.y - screenFrame.origin.y;` and `dockRight = screenFrame.size.width - visibleFrame.size.width;` – Jeff Jul 18 '20 at 03:04
  • 1
    @Jeff thanks for the answer, but it does not work for the left/right/width, since it is always screenFrame.size.width == visibleFrame.size.width and visibleFrame.origin.x == screenFrame.origin.x – IGHOR Jul 18 '20 at 13:00
  • @IGHOR In my case, I only needed one of those values (left dock offset, right dock offset, or bottom dock offset) and I could compute that one value based on the first true condition in those `if` statements. I'm using an iMac 27-inch with one screen. – Jeff Jul 20 '20 at 04:11
  • 1
    @Jeff I understand but left/right offset of your code is always zero or equal to a screen width. Are you sure this code gives you a real Dock position offset if dock is on Bottom? It is only possible to calculate Height of the Dock this way. – IGHOR Jul 20 '20 at 10:46
0

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.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • I think I can use Cocoa or Carbon API in Qt C++ application, so Qt is not problem. Take a screenshot and image recognition could works, but it not intelligent solution and it may not works in many situations. – IGHOR Mar 17 '16 at 00:37
  • I haven't found anything about accessing dock properties from the apple APIs. – dtech Mar 17 '16 at 08:51