AppleScript Library

Display Library

   Retrieve some information on the connected displays.

All handlers displayed on this page were freely available on the internet or written by myself. Please refer to the copyright section at the end of this page. Feedback appreciated.


AppleScript Editor open download

Properties
myName
Display Info
getMainDisplayResolution
getDisplayInfos

Properties

property myName

Name that should be used when loading this library.

show source AppleScript-Editor open insert append toc
property myName : "_display"

Display Info

getMainDisplayResolution()

Return screen resolution of main display (written and tested on Mac OS 10.6).

listlist of integers (width, heigt)

Written by:ljr

Example:

getMainDisplayResolution() 
--> {1920, 1200}
show source AppleScript Editor open insert append toc
on getMainDisplayResolution()
	local tmp, w, h
	try
		set tmp to (do shell script ¬
			"defaults read /Library/Preferences/com.apple.windowserver| " & ¬
			"egrep -w 'Height|Width'")
		set h to tmp's word 3 as integer
		set w to tmp's word 6 as integer
		return {w, h}
	on error eMsg number eNum
		error "Can't getMainDisplayResolution: " & eMsg number eNum
	end try
end getMainDisplayResolution

getDisplayInfos()

Return a list of records containing information on connected displays (clumsy and not very fast: ~0.2 secs, written and tested on Mac OS 10.6).

listlist of records

Written by:ljr

Example:

getDisplayInfos() 
--> {{name:"SyncMaster", resolution:{1920, 1200}, mainDisplay:true, rotationSupported:true}}
show source AppleScript Editor open insert append toc
on getDisplayInfos()
	local res, len, idxList, tmpList, displayList, thisLine, displayCount
	local end_, start_, i, j, rec, ASTID
	set ASTID to AppleScript's text item delimiters
	try
		set res to do shell script "system_profiler SPDisplaysDataType"
		set AppleScript's text item delimiters to "      Displays:\r"
		set res to res's text item 2
		set AppleScript's text item delimiters to ASTID
		log res
		set res to res's paragraphs
		set len to count res
		set idxList to {}
		set tmpList to {}
		set displayList to {}
		repeat with i from 1 to len
			set thisLine to res's item i as text
			try
				if thisLine starts with "        " and ¬
					thisLine's text 9 is not space then
					set end of idxList to i
				end if
			end try
		end repeat
		set displayCount to count idxList
		repeat with i from 1 to displayCount
			set start_ to idxList's item i
			if i = displayCount then
				set end_ to len - 1
			else
				set end_ to idxList's item (i + 1)
			end if
			set end of tmpList to res's items start_ thru end_
		end repeat
		repeat with i from 1 to displayCount
			set thisList to tmpList's item i
			set len to count thisList
			set rec to {name:"", resolution:{}, mainDisplayfalse, rotationSupported:false}
			set rec's name to (thisList's item 1)'s text 9 thru -2
			if rec's name is not "Display Connector:" then
				repeat with j from 2 to len
					set thisLine to thisList's item j
					if thisLine contains "Main Display: Yes" then
						set rec's mainDisplay to true
					else if thisLine contains "Resolution:" then
						set thisLine to thisLine's text 22 thru -1
						set rec's resolution to ¬
							{thisLine's word 1 as integer, ¬
								thisLine's word 3 as integer}
					else if thisLine contains "Rotation: Supported" then
						set rec's rotationSupported to true
					end if
				end repeat
				set end of displayList to rec
			end if
		end repeat
		return displayList
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't getDisplayInfos: " & eMsg number eNum
	end try
end getDisplayInfos

Copyright

© 2008 ljr (http://applescript.bratis-lover.net)
getDisplayInfos, getMainDisplayResolution

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.