Skip to content

Instantly share code, notes, and snippets.

@rgov
Created October 13, 2011 00:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgov/1283043 to your computer and use it in GitHub Desktop.
Save rgov/1283043 to your computer and use it in GitHub Desktop.
Zoom Pages document to actual size.
(* Converts hex digits in big endian to an integer. *)
on fromHex(theDigits)
set theValue to 0
repeat with theDigit in theDigits
set theValue to (theValue * 16) + (offset of theDigit in "0123456789ABCDEF") - 1
end repeat
return theValue
end fromHex
(* Does a record have a certain key? (Hack.) *)
on hasKey(theKey, theDict)
set theScript to "on run {theDict}" & return & "return " & theKey & " of theDict" & return & "end run"
try
run script theScript with parameters {theDict}
return true
on error number -1728
return false
end try
end hasKey
(* A "class" representing a display on screen. *)
script Display
property name : "Unnamed Display"
property edid : ""
property width : 0 (* Physical width, in inches. *)
property height : 0 (* Physical height, in inches. *)
property horz : 0 (* Horizontal resolution, in pixels. *)
property vert : 0 (* Vertical resolution, in pixels. *)
property isMain : false (* Is this the main display? *)
(* Computes the diagonal length of the screen. *)
on getDiagonal()
return ((my height) ^ 2 + (my width) ^ 2) ^ 0.5
end getDiagonal
(* Computes the density of the screen, in pixels per inch. *)
on getDensity()
return ((my horz) * ((1 + ((my vert) ^ 2) / ((my horz) ^ 2)) ^ 0.5)) / (getDiagonal())
end getDensity
(* Extracts useful values from the system_profiler(8) display dictionary. *)
on initWithDict(theDict)
set my name to _name of theDict
-- Is this the main display?
if hasKey("spdisplays_main", theDict) and spdisplays_main of theDict is "spdisplays_yes" then
set my isMain to true
end if
-- Extract the EDID.
set my edid to items 3 thru end of _spdisplays_edid of theDict
-- Parse the width and height and convert to inches.
set {my width, my height} to {fromHex(items 43 thru 44 of my edid) * 0.3937, fromHex(items 45 thru 46 of my edid) * 0.3937}
-- Parse the vertical and horizontal resolution.
set AppleScript's text item delimiters to " x "
set {my horz, my vert} to (every text item of spdisplays_resolution of theDict)
set AppleScript's text item delimiters to ""
end initWithDict
(* Constructor *)
on make with data theDict
copy me to newObj
tell newObj to initWithDict(theDict)
return newObj
end make
end script
(* Builds a list of displays attached to this system. *)
on getDisplays()
set theDisplays to {}
-- Run system_profiler(8) and parse its output
do shell script "/usr/sbin/system_profiler -xml SPDisplaysDataType"
tell application "System Events"
set thePlist to make new property list item with data the result
set theData to value of property list item 1 of (make new property list item with data the result)
delete thePlist -- Free up memory
end tell
set graphicsCards to _items of theData
repeat with theGPU in graphicsCards
if hasKey("spdisplays_ndrvs", theGPU) then
set gpuDisplays to spdisplays_ndrvs of theGPU
repeat with displayDict in gpuDisplays
set the end of theDisplays to make Display with data displayDict
end repeat
end if
end repeat
return theDisplays
end getDisplays
(*
Here's the entry point of the script.
We get all of our displays, look for the main one, and use it.
*)
on run
set myDisplays to getDisplays()
-- Filter to find the main display.
set mainDisplay to missing value
repeat with theDisplay in myDisplays
if isMain of theDisplay is true then
set mainDisplay to theDisplay
exit repeat
end if
end repeat
if mainDisplay is missing value then
display dialog "Could not find the main display."
return
end if
-- Some logging.
set theDiagonal to getDiagonal() of mainDisplay
set theDensity to getDensity() of mainDisplay
log "I'm using the display named " & quoted form of name of mainDisplay & ", which is " & width of mainDisplay & "in x " & height of mainDisplay & "in (" & theDiagonal & "in diag.) and " & horz of mainDisplay & "x" & vert of mainDisplay & " at " & theDensity & "ppi."
-- Rescale the document appropriately
tell application "Pages"
set view scale of the first window to (theDensity * 100.0) / 72.0
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment