Last active
August 7, 2023 16:38
-
-
Save briankc/025415e25900750f402235dbf1b74e42 to your computer and use it in GitHub Desktop.
Get display ID from NSScreen for use with CGDirectDisplay API
This file contains hidden or 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
import Cocoa | |
import CoreGraphics | |
extension NSScreen { | |
var displayID: CGDirectDisplayID { | |
return deviceDescription["NSScreenNumber"] as? CGDirectDisplayID ?? 0 | |
} | |
} |
This file contains hidden or 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
import Cocoa | |
import CoreGraphics | |
extension NSScreen { | |
var displayID: CGDirectDisplayID? { | |
return deviceDescription["NSScreenNumber"] as? CGDirectDisplayID | |
} | |
} |
now Swift 5 requires this:
extension NSScreen {
var displayID: CGDirectDisplayID? {
return deviceDescription[NSDeviceDescriptionKey(rawValue: "NSScreenNumber")] as? CGDirectDisplayID
}
}
nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The latter option is the better way to go, IMHO.