tags:

views:

19

answers:

2

Hi,

I'm trying to read the resolutions & positions of currently connected screen. Eg, if I had 2 screens connected, I may get information like...

Screen 1, 1920*1080, x:0, y:0 Screen 2, 1280*1024, x:1920, y:0

From my own bumbling research, I think /Library/Preferences/com.apple.windowserver.plist may have what I'm looking for, but it seems to contain information about more screens than those connected, and I can't see a way to tell which is which.

Ideally I'd like to get this information with Python, but interested in hearing any solutions.

I'm from a Windows background, so if I'm coming at things the wrong way that might be why

Many thanks!

A: 

I don't know about in Python, but since you said you're interested in any solutions, you can do this in Java with GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(). This will return an array of screens, which you can loop through and get various properties of. For example, you could print the width and height of the first screen by doing something like the following:

System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());

The obvious advantage of doing this within the constructs of whatever language you choose instead of by parsing a system file is that it should be portable, but that might not matter to you since you specifically requested OS X.

EDIT: I just read that .getScreenDevices() provides no guarantee that the array will be populated in any particular order (in regards to screen position), so this may not fully suit your needs.

Segphault
Interesting, will this give me the offset of the screens too? As in tell me where the screen appear relative to each other? That's the main bit I'm having trouble getting hold of. As in, can I tell if the 2nd monitor is aligned to the bottom-right of the first etc etc with pixel precision?
Jaffa The Cake
No, see my edit -- for that info, I suspect you will have to parse a system file of some sort after all. Unfortunately, I don't know where to look.
Segphault
+1  A: 

The official API for this is NSScreen. In Objective-C, you use

NSArray*screens=[NSScreen screens];
for(NSScreen*screen in screens){
     NSRect frame=[screen frame];
     // frame.origin.{x,y} and frame.size.{height,width} contains the info you want
}

The 0-th entry of the array [NSScreen screens] is guaranteed to be the main screen, i.e. with the main menu bar.

Using Cocoa methods from Python should be easy, although I don't know :p From the command line,

$ python
>>> from AppKit import *
>>> NSScreen.screens()[0].frame()

gave the screen dimensions... although I don't fully understand how the bridging works. Read the official doc!

Yuji
Thanks muchly, exactly what I'm looking for. Seems I was confusing myself trying to do this via applescript with appscript.
Jaffa The Cake