10

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?)

2
  • 2
    it's because you use windows features to do stuff instead of cross platform lib. You are locking yourself into a trap. For picture manipulation, use PIL. Commented Aug 15, 2009 at 9:49
  • Alan: check this out: developer.apple.com/graphicsimaging/pythonandquartz.html Commented Aug 15, 2009 at 21:36

4 Answers 4

13

With Pyobjc something like this should work. Pyobjc comes with Leopard.

from AppKit import NSScreen
print(NSScreen.mainScreen().frame())

With that, you can also grab the width and height.

NSScreen.mainScreen().frame().size.width
NSScreen.mainScreen().frame().size.height

For example:

print("Current screen resolution: %dx%d" % (NSScreen.mainScreen().frame().size.width, NSScreen.mainScreen().frame().size.height))
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, pyobjc is quite something. And it's on every mac since Leopard. And remember when actually using this a lot of mac users have more then one screen. So account for that when needed. See Apple's docs on NSScreen for more info.
8

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) 

1 Comment

This worked for me, once Quartz was installed with pip install pyobjc-framework-Quartz
2

As usual, using features that are binded to an OS is a very bad idea. There are hundred of portable libs in Python that give you access to that information. The first that comes to my mind is of course pygame :

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640,480), FULLSCREEN)
x, y = screen.get_size()

But I guess cocoa do just as good, and therefor wxpython or qt is your friend. I suppose on Windows you did something like this :

from win32api import GetSystemMetrics
width = GetSystemMetrics [0]
height = GetSystemMetrics [1]

Sure it's easier, but won't work on Mac, Linux, BSD, Solaris, and probably nor very later windows version.

2 Comments

The pygame example was not useful to me because you are getting the resolution of the pygame screen, not your physical screen. I don't know pygame very well so let me know if I missed something.
Your code took my screen into low resolution game window without any ability to exit. I was forced to reboot my Mac! Your concept is correct - we need a portable code, but the solution is bad because pygame.init() is opening a game window which we don't need.
2

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

1 Comment

This does not work for me, system_profiler SPDisplaysDataType outputs the maximum resolution of my screen, not the current resolution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.