Reading main display resolution

How can I read the resolution of display 1 in System Preferences in a script?
The value I am lloking for is the one selected on the Resolutions list on the Display tab. In my display 1280x1024
It would be really nice if I could get this blind ie without having the display pane come up.


-- tell application "System Preferences" to activate

tell application "System Events"
	tell process "System Preferences"
		try
			set myRes to resolution of window 1
		on error e
			return "error: " & e
		end try
	end tell
end tell

(*ignoring application responses
	tell application "System Preferences" to quit
end ignoring*)

"error: System Events got an error: Can't make resolution of window 1 of process \"System Preferences\" into type reference."

Hi,

try this


set pListpath to (path to library folder as text) & "Preferences:com.apple.windowserver.plist"
tell application "System Events"
	tell property list item 1 of property list item 1 of property list item "DisplaySets" of contents of property list file pListpath
		set {dispHeight, dispWidth} to {value of property list item "Height" as integer, value of property list item "Width" as integer}
	end tell
end tell

StefanK
Thanks for that.
It works exactly as I asked, you rock.
This would work under most circumstances.
As I need this to work unsupervised under any circumstance I deleted com.apple.windowserver.plist
As one would expect the script failed.
I had the Mac rebuild com.apple.windowserver.plist by changing the resolution of the display and it was recreated from scratch.
Any other way of rebuilding the file without the user noticing? ie the screen going blue etc.

I am trying to make a finder window which is as tall and wide as the main display.
I have solved the way of getting the width of the main display by finding out the width of the menu bar which is always there and never differs from the width of the main display.
For the height I am kind of stuck. The obvious way would be to get it from the bounds of window of desktop. That would work if I had only one screen connected.
I have a second display a lot taller than the main one and the bounds of window of desktop unfortunately give me the height as the tallest display making my window too tall.

you can get the screen resolution also with the CLI cscreen

There is also this approach:

-- Adapted from script by Nigel Garvey
set f to (path to preferences from local domain as Unicode text) & "com.apple.windowserver.plist"
tell application "System Events"
	try -- if two screens
		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
	on error -- only one screen
		set {|Width|:w1} to value of item 1 of property list items of property list item 1 of property list item "DisplaySets" of property list file f
		set {w2, h2} to {0, 0}
	end try
end tell
{{w1, h1}, {w2, h2}}

I also often need to know the user’s current screen size in my scripts and therefor wrote myself a little command line tool in Obj-C. The small utility named «screensize» is based on the NSScreen class and can be easily called from within an AppleScript using the «do shell script» command. If you are interested in the (most simple) source code, you can study it right here. Or just download the compiled tool.

Once you unzipped it, you can place it into the script/application bundle of your AppleScript:


et toolpath to ((path to desktop) as Unicode text) & "screensize"
-- set toolpath to ((path to me) as Unicode text) & "Contents:Resources:screensize"
set output to do shell script (quoted form of POSIX path of toolpath)
set {dspwidth, dspheight} to {word 1, word 3} of output
-- {"1440", "900"}

Thanks to you all guys, problem elegantly solved. :slight_smile: