Skip to content

Instantly share code, notes, and snippets.

@wonderbit
Created November 28, 2016 19:04
Show Gist options
  • Save wonderbit/c8896ff429a858021a7623f312dcdbf9 to your computer and use it in GitHub Desktop.
Save wonderbit/c8896ff429a858021a7623f312dcdbf9 to your computer and use it in GitHub Desktop.
Get the Dock position, size and hidden state in a Cocoa app
//
// 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
}
}
@Kinark
Copy link

Kinark commented Mar 26, 2023

You’re not getting it’s size, you’re getting it’s height.

@wonderbit
Copy link
Author

Yeah, but if it's positioned left or right, it's the width…

@soundflix
Copy link

Xcode 14.2 I needed the following changes:
import AppKit
NSScreen.main() is now NSScreen.main

@hugows
Copy link

hugows commented Jun 26, 2024

@sdsykes
Copy link

sdsykes commented Jun 26, 2024

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