{"id":166,"date":"2007-09-24T17:40:58","date_gmt":"2007-09-24T22:40:58","guid":{"rendered":"http:\/\/byrneweb.com\/sunburn\/blog\/2007\/09\/24\/applescript-for-display-size\/"},"modified":"2007-09-24T18:12:05","modified_gmt":"2007-09-24T23:12:05","slug":"applescript-for-display-size","status":"publish","type":"post","link":"http:\/\/byrneweb.com\/sunburn\/blog\/2007\/09\/applescript-for-display-size\/","title":{"rendered":"AppleScript for Display Size"},"content":{"rendered":"<p>As noted, I use AppleScript here and there. One of the annoying things about AppleScript is that you can\u2019t natively just ask \u201chow big is the main monitor?\u201d  There are workarounds, such as <a href=\"http:\/\/daringfireball.net\/2006\/12\/display_size_applescript_the_lazy_way\">this<\/a>, but as noted, it doesn\u2019t necessarily handle multiple monitors properly. Since I almost always run with multiple monitors, that\u2019s not a good solution for me.<\/p>\n<p>Well, turns out someone solved this a long time ago, but that solution is <a href=\"http:\/\/lists.apple.com\/archives\/AppleScript-Users\/2003\/Aug\/msg00080.html\">well-hidden<\/a>, and it relies on calling a shell script:<\/p>\n<p><span style=\" color: #0000DD;\"><strong>on<\/strong><\/span> <span style=\"\"><\/span><span style=\" color: #408000;\">screenHeight<\/span><span style=\"\">()<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(<\/span><span style=\" color: #0000FF;\">word<\/span> <span style=\"\"><\/span><span style=\" color: #555555;\">3<\/span> <span style=\"\"><\/span><span style=\" color: #0000DD;\"><strong>of<\/strong><\/span> <span style=\"\">(<\/span><span style=\" color: #0000FF;\">do shell script<\/span> <span style=\"\"><\/span><span style=\"font-size: 9pt;\">\u00ac<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<\/span><span style=\"\">&#8220;<\/span><span style=\" color: #555555;\">defaults read \/Library\/Preferences\/com.apple.windowserver | grep -w Height<\/span><span style=\"\">&#8220;)) <\/span><span style=\"font-size: 9pt;\">\u00ac<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<\/span><span style=\" color: #0000DD;\"><strong>as<\/strong><\/span> <span style=\"\"><\/span><span style=\" color: #0000FF;\">number<\/span><br \/>\n<span style=\" color: #0000DD;\"><strong>end<\/strong><\/span> <span style=\"\"><\/span><span style=\" color: #408000;\">screenHeight<\/span><\/p>\n<p>Hunh?<\/p>\n<p>It\u2019s not that bad. The \u2018defaults read\u2019 part of that just reads from the preferences file. What makes this work is that the \u201ccom.apple.windowserver\u201d 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 \u201cdefaults read \/Library\/Preferences\/com.apple.windowserver\u201d in the Terminal) is piped into grep, the item found is the height of the main monitor. Cool.<\/p>\n<p>If you want the width of the main monitor, just substitute \u201cWidth\u201d for \u201cHeight\u201d in the above mini-script.<\/p>\n<p>Incidentally, the way I used to solve this (rather than relying on reading the size of the desktop) relied on the <a href=\"http:\/\/www.seanet.com\/~jonpugh\/\">Jon\u2019s Commands<\/a> set of osaxen, but those break on Intel machines. Bummer, as they\u2019re amazingly useful&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As noted, I use AppleScript here and there. One of the annoying things about AppleScript is that you can\u2019t natively just ask \u201chow big is the main monitor?\u201d There are workarounds, such as this, but as noted, it doesn\u2019t necessarily handle multiple monitors properly. Since I almost always run with multiple monitors, that\u2019s not a &hellip; <a href=\"http:\/\/byrneweb.com\/sunburn\/blog\/2007\/09\/applescript-for-display-size\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">AppleScript for Display Size<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/posts\/166"}],"collection":[{"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":0,"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/byrneweb.com\/sunburn\/blog\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}