September 24th, 2007
As noted, I use AppleScript here and there. One of the annoying things about AppleScript is that you can’t natively just ask “how big is the main monitor?” There are workarounds, such as this, but as noted, it doesn’t necessarily handle multiple monitors properly. Since I almost always run with multiple monitors, that’s not a good solution for me.
Well, turns out someone solved this a long time ago, but that solution is well-hidden, and it relies on calling a shell script:
(word 3 of (do shell script ¬
“defaults read /Library/Preferences/com.apple.windowserver | grep -w Height“)) ¬
It’s not that bad. The ‘defaults read’ part of that just reads from the preferences file. What makes this work is that the “com.apple.windowserver” prefs file is created and updated by the system, and the first entry in this file is the main monitor! Thus, when the output of that command (you can see the record by just running “defaults read /Library/Preferences/com.apple.windowserver” in the Terminal) is piped into grep, the item found is the height of the main monitor. Cool.
If you want the width of the main monitor, just substitute “Width” for “Height” in the above mini-script.
Incidentally, the way I used to solve this (rather than relying on reading the size of the desktop) relied on the Jon’s Commands set of osaxen, but those break on Intel machines. Bummer, as they’re amazingly useful…