Skip to content

Instantly share code, notes, and snippets.

@justvanrossum
Created June 6, 2019 06:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justvanrossum/9843bf52d93cbe1c7a3f37420bea8d34 to your computer and use it in GitHub Desktop.
Save justvanrossum/9843bf52d93cbe1c7a3f37420bea8d34 to your computer and use it in GitHub Desktop.
macOS/Python: Get dimension info about active screens
from AppKit import NSScreen, NSDeviceSize, NSDeviceResolution
from Quartz import CGDisplayScreenSize
for i, screen in enumerate(NSScreen.screens(), 1):
description = screen.deviceDescription()
pw, ph = description[NSDeviceSize].sizeValue()
rx, ry = description[NSDeviceResolution].sizeValue()
mmw, mmh = CGDisplayScreenSize(description["NSScreenNumber"])
scaleFactor = screen.backingScaleFactor()
pw *= scaleFactor
ph *= scaleFactor
print(f"display #{i}: {mmw:.1f}×{mmh:.1f} mm; {pw:.0f}×{ph:.0f} pixels; {rx:.0f}×{ry:.0f} dpi")
@justvanrossum
Copy link
Author

Example output:

display #1: 609.6×345.1 mm; 3840×2160 pixels; 72×72 dpi
display #2: 286.4×179.0 mm; 3360×2100 pixels; 144×144 dpi

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