AppleScript for Display Size

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:

on screenHeight()
word 3 of (do shell script ¬
defaults read /Library/Preferences/ | grep -w Height“)) ¬
as number
end screenHeight


It’s not that bad. The ‘defaults read’ part of that just reads from the preferences file. What makes this work is that the “” 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/” 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…

2 thoughts on “AppleScript for Display Size”

  1. That’s very cool! I only wish that had come up with a Google search… Thanks for the tip!

Comments are closed.