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?
On your Mac, choose Apple menu > System Preferences, then click Dock & Menu Bar . In the Dock & Menu Bar section in the sidebar, change the options you want. For example, you can change how items appear in the Dock, adjust its size, locate it along the left or right edge of the screen, or even hide it.
Drag the slider to change the Dock size. Magnify icons when you move the pointer over them. Drag the slider to choose the magnification size.
Adding Folders to the Dock A vertical line separates the app icons on the left side of the Dock from folders, the Trash and minimized windows on the right side of the Dock.
Today's Dock is split into three sections, marked by dividing lines: Handoff, apps, and a third area that contains documents, folders, and the Trash.
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 systemStatusBar].thickness
will return the height of the Menu bar.
https://developer.apple.com/reference/appkit/nsscreen/1388369-visibleframe?language=objc
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With