开发者

Getting screen resolutions & position/offsets from OSX

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 th开发者_如何学编程e wrong way that might be why

Many thanks!


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜