a reliable way to get information about your displays

I found getting information about your displays to be very unreliable using applescript methods. I have 2 displays and often get misinformation from reading plist files. As such I have written a unix executable file which queries the Quartz Display Services of MacOSX to retrieve reliable information. When used in combination with this script you should no longer have any problems getting reliable information about your displays. I tried using “cscreen”, but it is missing one vital piece of information that I need… the display ID. More details are included in the script.

NOTE: to use this applescript you need to do 2 things.

  1. download the unix executable file “displaysInfo” to your computer. You can get it here: http://rapidshare.com/files/54473723/displaysInfo.zip
  2. put the POSIX path to the “displaysInfo” unix executable file into the first line of the “displaysInfo()” handler
(* NOTE: to use this applescript you need to do 2 things.
1) download the unix executable file "displaysInfo" to your computer. You can get it here: http://rapidshare.com/files/54473723/displaysInfo.zip
2) put the POSIX path to the "displaysInfo" unix executable file into the first line of the "displaysInfo()" handler *)

(* this script uses the unix executable "displaysInfo" to get information about your displays from the Quartz Display Services of MacOSX... so it should always be completely accurate *)
(* I wrote the unix file and this script because I found that reading from the com.apple.windowserver plist files to be extremely unreliable. In my 2-display setup I often got misinformation reading from the plist files because 1) there's multiple plist files (one global and one in each users "byHost" folder) and their contents do not match with each other 2) the structure of all the plist files change and 3) there's multiple sets of display parameters in each plist file so your not sure which set to read. *)

(*the information returned about each display is as follows:
displayNum:  number of the display, number 1 is the main display (i.e. with menu bar)
displayID:  ID number of the display
theResolution:  resolution of the display
bitDepth:  bit depth of the display, note: 32 means your monitor is displaying millions of colors, 16 is thousands, and 8 is 256
refreshRate:  refresh rate of the display in hertz
desktopPosition:  position of the display's desktop in global coordinates. note: the upper left corner of the main display is always {0,0} and the rest of the coordinates are measured from that position. *)

-- an example of how to use the data: if you wanted the displayID of the main monitor... 
-- set displayID to displayID of (item 1 of displaysInfo)  --> the "item number" is the number of the display you want the information from!


set displays_info to displaysInfo()

on displaysInfo()
	set displaysInfoParas to paragraphs of (do shell script "/usr/local/bin/displaysInfo | cut -d : -f 2") --> the path to the unix executable
	set displaySets to {}
	repeat with i from 0 to ((count of displaysInfoParas) div 10 - 1)
		tell items (1 + (i * 10)) thru (10 + (i * 10)) of displaysInfoParas
			set end of displaySets to {displayNum:item 1 as number, displayID:item 2, theResolution:{item 3 as number, item 4 as number}, bitDepth:item 10 as number, refreshRate:item 9 as number, desktopPosition:{{item 5 as number, item 6 as number}, {item 7 as number, item 8 as number}}}
		end tell
	end repeat
	return displaySets
end displaysInfo

Hi regulus,

in my current version :wink: of the StrechFrontWindow script I use
a shorter way to create the record


set displaysInfo to displaysInfo()

on displaysInfo()
	set displaysInfoParas to paragraphs of (do shell script "/usr/local/bin/displaysInfo | cut -d : -f 2") --> the path to the unix executable
	set displaySets to {}
	repeat with i from 0 to ((count of displaysInfoParas) div 10 - 1)
		set end of displaySets to getDisplaySet(items (1 + (i * 10)) thru (10 + (i * 10)) of displaysInfoParas)
	end repeat
	return displaySets
end displaysInfo

on getDisplaySet(p)
	tell p
		return {displayNum:item 1 as number, displayID:item 2, theResolution:{item 3 as number, item 4 as number}, bitDepth:item 10 as number, refreshRate:item 9 as number, desktopPosition:{{item 5 as number, item 6 as number}, {item 7 as number, item 8 as number}}}
	end tell
end getDisplaySet

Thanks StefanK! I like that better. I updated my original script above with your version, although I modded it to make it into one handler instead of 2.

Please update your StrechFrontWindow script with this new link because I changed the file on RapidShare so the old version no longer exists…
http://rapidshare.com/files/54473723/displaysInfo.zip

I’ve changed it.

PS: You can do it also this way

on displaysInfo()
	set displaysInfoParas to paragraphs of (do shell script "/usr/local/bin/displaysInfo | cut -d : -f 2") --> the path to the unix executable
	set displaySets to {}
	repeat with i from 0 to ((count of displaysInfoParas) div 10 - 1)
		tell items (1 + (i * 10)) thru (10 + (i * 10)) of displaysInfoParas
			set end of displaySets to {displayNum:item 1 as number, displayID:item 2, theResolution:{item 3 as number, item 4 as number}, bitDepth:item 10 as number, refreshRate:item 9 as number, desktopPosition:{{item 5 as number, item 6 as number}, {item 7 as number, item 8 as number}}}
		end tell
	end repeat
	return displaySets
end displaysInfo

Yes! Even shorter! That’s the best! :slight_smile: