It errors in Jaguar (“Illegal operation”) but returns a list of numeric Unicode texts in Tiger. For a single screen, we’d need something like this:
tell (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | sed -Ee '/^ *(Height|Width)/!d' -e 's/[^[:digit:]]*//g'") to set screenRes to {paragraph 2 as integer, paragraph 1 as integer}
Interestingly enough, after posting that, I tried changing the value to “1” to see what would happen and actually turned into an alien for a while. Fortunately, I was able to slither into a vat of dinitrogen tetrasulphide while I restored the backup and, after a good night’s sleep and a cup of tea, am apparently no worse off for the experience. But thank goodness I didn’t try the “2” setting! :o
After fiddling around for a short while, I’ve finally settled on this. It should hopefully return a list of records, but unfortunately I’m unable to test it on more than one display.
run script ("{" & (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | sed -Ee '/^ *(Height|Width)/!d' -e 's/Height = (.*);/{height: \\1,/' -e 's/Width = (.*);/width: \\1},/' -e 's/ *$/¬/'")'s text 1 thru -3 & "}")
I presume that, as the last one died of an obscure error, this too will fail on Jaguar.
Edit: Thanks Stefan, my fault. The script has been changed.
FWIW, Extra Suites may come in handy when changing screenresolutions AND depth on a fly (here for single screen):
tell application "Extra Suites" to set g to ES list screen resolutions screen 0
set y to {}
repeat with x in g
if x is g's last item as string then set x to " depth " & x
set y to y & my substitute(x, "x", "*")
end repeat
tell application "System Events" to set fApp to some application process whose frontmost is true
beep 2
tell fApp
activate
set fWd to name of window 1
set fWd2 to name of front window
set nRes to (choose from list y default items {item 3 of y} with prompt "Set screen resolution to: ")
end tell
if nRes = false then return
set nRes to nRes as string
set {nH, nV, dept} to {nRes's word 1, nRes's word 3, nRes's word 5} --return {nH, nV}
tell application "Extra Suites"
set sInfo to ES screen info
if width of sInfo ≠nH and height of sInfo ≠nV then
ES set screen resolution width nH height nV depth dept
end if
set sInfo to ES screen info
set {W, H} to {sInfo's width, sInfo's height}
end tell
tell fApp
activate
try
open window 1
end try
try
open window fWd
end try
try
open window fWd2
end try
return name of window 1
end tell
on substitute(theText, toReplace, newText)
set AppleScript's text item delimiters to the toReplace
set the allTheText to every text item of theText
set AppleScript's text item delimiters to the newText
set theText to the allTheText as string
set AppleScript's text item delimiters to ""
return theText
end substitute
An AppleScript list of records! Terrific! The list I get on my Tiger machine contains seventeen records, the first of which and six more refer to the single monitor’s current resolution. The remaining ten refer to another resolution to which I’ve set it in the past. None of the other possible resolutions are included, so this can’t be the right file for that information. Only the first one or two entries can thus have any relevance.
The script still doesn’t work with Jaguar, I’m afraid. But I must apologise to you for some bad information. The error isn’t “Illegal operation” but “illegal option”. (Perhaps the interocitor interfered with my brainwaves the other evening!) In full, the message is:
Works for me with dual screens, QD, returning all of the resolutions possible with both screens. The resolution of the screen with the menu bar is listed first and the secondary screen’s is listed second, but I don’t know what order the remaining entries have.
One method that we didn’t explore here was System Events’s “Property List Suite” “ mainly because this was only introduced with Tiger and the original poster wanted a script that would work with earlier systems too. But I’ve been having some fun today with System Events’s amazingly flexible references and thought I’d add this for completeness.
Edit: Long and very interesting post about the references snipped for being inapplicable in the current context. (See the immediately following exchange between Adam and myself.) Instead, here’s a single Property List script that returns the screen resolution(s) of up to two attached monitor(s):
set f to (path to preferences from local domain as Unicode text) & "com.apple.windowserver.plist"
tell application "System Events"
set v to value of property list item 1 of property list item "DisplaySets" of property list file f
set {scrRes1, scrRes2} to {{|Width|, |Height|} of beginning of v, missing value}
if ((count v) > 1) then set scrRes2 to {|Width|, |Height|} of item 2 of v -- Second monitor.
end tell
{scrRes1, scrRes2} --> {{1680.0, 1050.0}, missing value} -- Single monitor.
Alas, Nigel, on MM all of these latest solutions return {1152, 870}, {1152, 870}, while in fact, my screens are {1152, 870}, {1024, 768}; a 19" and a 17" diagonal CRT running off the dual outputs on my stock (NVIDA GeForce 6600) video card. :rolleyes:
Thanks for the feed-back. If that’s the case, it sounds as though everything that’s been discussed in this thread is inherently flaky. The scripts all rely on getting the first two “Height” and “Width” settings in the preference file “ which is exactly what these Property List scripts do. However, I don’t have two monitors. Perhaps the settings are grouped differently when there are two monitors attached to one machine. There might be a clue in that ‘property list item 1’ in the middle of the reference. Maybe there’s a ‘property list item 2’ when there’s a second monitor. Does this work?
set f to (path to preferences from local domain as Unicode text) & "com.apple.windowserver.plist"
tell application "System Events"
set {{|Width|:w1, |Height|:h1}, {|Width|:w2, |Height|:h2}} to value of property list items of property list item 1 of property list item "DisplaySets" of property list file f
end tell
{{w1, h1}, {w2, h2}}
And have you ever noticed that the clock on this BBS is about three and a half minutes fast?