All entries for Monday 28 December 2009
December 28, 2009
Find screen resolution from Mac OS X command line.
Today I found myself wondering if it was possible to find the current screen resolution from the command line (and hence from a shell script) in Mac OS X. My first thought was to try the same method I use in Linux which is extract it from the output of xdpyinfo. That turned out to be no use though. Firstly running xdpyinfo causes X11.app to launch, which is messy. Second, I rather suspect that if X11.app isn't installed then neither is xdpyinfo and X11 is an optional install pre-Leopard. Finally, it gives me the wrong value for the vertical resolution. My screen resolution is 16080x1050 but xdpyinfo says:
case:~ mike$ xdpyinfo | grep dimensions
dimensions: 1680x1028 pixels (445x272 millimeters)
It's probably incorrect to say that it's giving the wrong value for the vertical resolution. More likely I expect is that it's the correct value for what it's measuring but what it's measuring is not what I want to measure. I find myself wondering if the Mac OS X Menu Bar (MenuBar? Menubar?) is 22 pixels high but I don't feel inclined to check.
I couldn't find any other method of getting the screen resolution via Google so the method I came up with to get the horizontal resolution is:
$ defaults read ~/Library/Preferences/ByHost/com.apple.windowserver.$(/sbin/ifconfig en0 | grep ether | cut -d " " -f 2 | sed 's/://g') | grep " Width =" | cut -d "=" -f 2 | sed 's/[ ;]//g'
To get vertical resolution replace 'Width' with 'Height'. It's rather long winded, but it works. I don't like the grep cut sed stuff at the end but I couldn't find a way to get the defaults command to directly read the relevant value.