Getting Screen Sizes in AppleScript

Having finally bit the bullet and upgraded to a new Mac running OS X 10.6, I find I need to replace some old Scripting Additions. Not wanting to fall into the trap again of relying on third parties, I will search for ways to accomplish the same tasks from within the system.

First up is a need to replace the “screen list” command in Jon’s Commands. This code gets the dimensions of the active displays.

set kCR to (character id 13)
set command to "/usr/sbin/system_profiler SPDisplaysDataType  | grep Resolution"
set output to (do shell script command)

set oldDelims to AppleScript's text item delimiters
   set myDisplays to {}
   set AppleScript's text item delimiters to {kCR} -- return
   set myDisplays to (text items of output)
   set AppleScript's text item delimiters to {": ", " x ", " @ "}
   repeat with a from 1 to (count myDisplays)
      set thisDisplay to rest of (text items of (item a of myDisplays))
      set (item a of myDisplays) to (thisDisplay)
   end repeat
end try
set AppleScript's text item delimiters to oldDelims
return myDisplays

Reply is {{w, h, hz}, {w, h, hz}}

Note also that I am taking advantage of the ability to define multiple text item delimiters: a Snow Leopard first!

Aug 24, 2010