Created
November 28, 2016 19:04
-
-
Save wonderbit/c8896ff429a858021a7623f312dcdbf9 to your computer and use it in GitHub Desktop.
Get the Dock position, size and hidden state in a Cocoa app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DockInfo.swift | |
// | |
// Created by Wessley Roche on 28/11/2016. | |
// | |
import Foundation | |
enum WBDockPosition: Int { | |
case bottom = 0 | |
case left = 1 | |
case right = 2 | |
} | |
func getDockPosition() -> WBDockPosition { | |
if NSScreen.main()!.visibleFrame.origin.y == 0 { | |
if NSScreen.main()!.visibleFrame.origin.x == 0 { | |
return .right | |
} else { | |
return .left | |
} | |
} else { | |
return .bottom | |
} | |
} | |
func getDockSize() -> CGFloat { | |
let dockPosition = getDockPosition() | |
switch dockPosition { | |
case .right: | |
let size = NSScreen.main()!.frame.width - NSScreen.main()!.visibleFrame.width | |
return size | |
case .left: | |
let size = NSScreen.main()!.visibleFrame.origin.x | |
return size | |
case .bottom: | |
let size = NSScreen.main()!.visibleFrame.origin.y | |
return size | |
} | |
} | |
func isDockHidden() -> Bool { | |
let dockSize = getDockSize() | |
if dockSize < 25 { | |
return true | |
} else { | |
return false | |
} | |
} |
There is also
defaults read com.apple.dock "orientation"
or the private API CoreDockGetOrientationAndPinning if that is something you can use. https://stackoverflow.com/questions/7717258/find-the-location-of-the-dock-programmatically
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Xcode 14.2 I needed the following changes:
import AppKit
NSScreen.main()
is nowNSScreen.main