Skip to content

Instantly share code, notes, and snippets.

@briankc
Last active August 7, 2023 16:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briankc/025415e25900750f402235dbf1b74e42 to your computer and use it in GitHub Desktop.
Save briankc/025415e25900750f402235dbf1b74e42 to your computer and use it in GitHub Desktop.
Get display ID from NSScreen for use with CGDirectDisplay API
import Cocoa
import CoreGraphics
extension NSScreen {
var displayID: CGDirectDisplayID {
return deviceDescription["NSScreenNumber"] as? CGDirectDisplayID ?? 0
}
}
import Cocoa
import CoreGraphics
extension NSScreen {
var displayID: CGDirectDisplayID? {
return deviceDescription["NSScreenNumber"] as? CGDirectDisplayID
}
}
@briankc
Copy link
Author

briankc commented Feb 28, 2017

The latter option is the better way to go, IMHO.

@ldenoue
Copy link

ldenoue commented Dec 19, 2022

now Swift 5 requires this:

extension NSScreen {
    var displayID: CGDirectDisplayID? {
        return deviceDescription[NSDeviceDescriptionKey(rawValue: "NSScreenNumber")] as? CGDirectDisplayID
    }
}

@LingYanSi
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment