Skip to content

Instantly share code, notes, and snippets.

@zats
Last active March 28, 2025 13:09
Show Gist options
  • Save zats/233d63b8e7845f62c7c200a359b3c076 to your computer and use it in GitHub Desktop.
Save zats/233d63b8e7845f62c7c200a359b3c076 to your computer and use it in GitHub Desktop.
Detect dock position on the screen for sandboxed macOS apps
import AppKit
struct DockInfo {
let position: DockPosition
let frame: CGRect
}
enum DockPosition {
case bottom
case left
case right
case hiddenOrUnknown
}
func detectDockInfo() -> DockInfo? {
for screen in NSScreen.screens {
let full = screen.frame
let visible = screen.visibleFrame
let deltaLeft = visible.minX - full.minX
let deltaBottom = visible.minY - full.minY
let deltaRight = full.maxX - visible.maxX
let deltaTop = full.maxY - visible.maxY // menu bar
// Assume menu bar is always at top, subtract that first
let menuBarHeight = deltaTop
if deltaBottom > 0 {
let height = deltaBottom
let frame = CGRect(x: full.minX, y: full.minY, width: full.width, height: height)
return DockInfo(position: .bottom, frame: frame)
} else if deltaLeft > 0 {
let width = deltaLeft
let frame = CGRect(x: full.minX, y: full.minY, width: width, height: full.height - menuBarHeight)
return DockInfo(position: .left, frame: frame)
} else if deltaRight > 0 {
let width = deltaRight
let frame = CGRect(x: full.maxX - width, y: full.minY, width: width, height: full.height - menuBarHeight)
return DockInfo(position: .right, frame: frame)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment