Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the desktop resolution in Mac via Python?

Tags:

python

macos

Trying to write a python application that downloads images from an RSS feed, and makes a composite background. How do I get the current desktop resolution on Mac OS X (leopard?)

like image 828
Alan Avatar asked Aug 15 '09 07:08

Alan


People also ask

How do I find out my Mac screen resolution?

On your Mac, choose Apple menu > System Settings, then click Displays in the sidebar. (You may need to scroll down.) Click the pop-up menu next to Resolution on the right, choose Scaled, then select one of the options.


2 Answers

If you are doing this from a LaunchAgent script, you may need to drop down to CoreGraphics primitives rather than AppKit-level methods. Working on this today, my LaunchAgent-loaded script gets None back from NSScreen.mainScreen(), but works fine if I load it from a terminal in my session.

from Quartz import CGDisplayBounds
from Quartz import CGMainDisplayID

def screen_size():
    mainMonitor = CGDisplayBounds(CGMainDisplayID())
    return (mainMonitor.size.width, mainMonitor.size.height) 
like image 136
Mike Rhodes Avatar answered Oct 13 '22 00:10

Mike Rhodes


I was having a hard time getting any of this to work so I looked around and put something together that seems to work. I am kinda new at coding so please excuse any errors. If you have any thoughts please comment.

results = str(subprocess.Popen(['system_profiler SPDisplaysDataType'],stdout=subprocess.PIPE, shell=True).communicate()[0])
res = re.search('Resolution: \d* x \d*', results).group(0).split(' ')
width, height = res[1], res[3]
return width, height
like image 22
eeskonivich Avatar answered Oct 12 '22 23:10

eeskonivich