Getting Screen Coordinates

I’m a newbie to AS, but from other languages, I know there are Toolbox calls: DMGetFirstScreenDevice and DMGetNexScreenDevice that return a text list showing {Number, Left, Top, Right, Bottom, Kind} - the coordinates of the corner of the active display(s). This is useful for calculating the offsets of the secondary screen so objects can be placed there. (If Kind is primary, the menu bar is there)

Is there some way to get this info in an AppleScript?

These are the items in my library:

getScreenResolution()

on getScreenResolution()
	set prefs to read alias ((path to startup disk as text) & "Library:Preferences:com.apple.windowserver.plist")
	set off1 to (offset of "<key>Width</key>" & (ASCII character 10) & ¬
		tab & tab & tab & tab & "<integer>" in prefs) + 30
	set off2 to (offset of "</integer>" & (ASCII character 10) & ¬
		tab & tab & tab & "</dict>" & (ASCII character 10) & ¬
		tab & tab & "</array>" & (ASCII character 10) & ¬
		tab & "</array>" & (ASCII character 10) & ¬
		"</dict>" & (ASCII character 10) & ¬
		"</plist>" & (ASCII character 10) & ¬
		"" in prefs) - 1
	set |width| to text from character off1 of prefs to character off2 of prefs as number
	set off1 to (offset of "<key>Height</key>" & (ASCII character 10) & ¬
		tab & tab & tab & tab & "<integer>" in prefs) + 31
	set off2 to (offset of "</integer>" & (ASCII character 10) & ¬
		tab & tab & tab & tab & "<key>IODisplayLocation" in prefs) - 1
	set |height| to text from character off1 of prefs to character off2 of prefs as number
	{|width|, |height|}
end getScreenResolution
{word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number, ¬
	word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number}
desktop_size()
on desktop_size()
	tell application "System Events"
		tell process "Finder"
			repeat with i from 1 to the count of windows
				if the position of window i is {0, 0} then
					return the size of window i
				end if
			end repeat
		end tell
	end tell
end desktop_size

My favorite is the first one :wink:

A real eye-opener - thank you. I hope some newbie questions are in order.

The second and third of these worked perfectly while the first balked with an error too large to see the bottom of. The error began:

Can't make "1152</integer>
			</dict>
			<dict>
				<key>Active</key>......

but I couldn’t read the bottom.
In my plist (found with File Buddy, opened from there in BBEdit), the string read (starting with ):
WidthLFtttt1152 etc. so the “+30” isn’t obvious. Clearly, it’s balking at trying to convert 1152 plus stuff that follows to integer.

In the terminal, I typed “defaults read /Library/Preferences/com.apple.windowserver” and I get back a {} delimited list, but the code only finds the first screen so I’d have to repeat it to find the next. Can do.

In the third, from which I could easily compute the offsets of the second screen with respect to the first (since it’s the whole desktop), I didn’t understand why you had to:


    tell application "System Events"
        tell process "Finder" ...

I assume that the repeat loop is there to figure out which of a left/right pair of displays holds the menu bar (window 1).

Again, thanks.

I forgot also these, but they require scripting additions:

screen size of (screen list)'s item 1 --> http://www.seanet.com/~jonpugh/
screen resolution --> http://www.osaxen.com/xtool.html