Monday, February 8, 2010

#1 2006-06-28 07:24:55 am

PJWilson
Member
Registered: 2005-08-01
Posts: 49

The definitive guide to screen resolution?

I hope that this is not a redundant thread on a well-covered topic. However, screen resolution is so widely covered in the forums (fora?), that I thought I would make a stab at tying together all the various methods in 1 posting. I have found the need to determine screen resolution frequently, and have yet to find a universal solution. Perhaps others might find this helpful, and maybe someone already has a universal solution...

P.S. I have tried all of these on my 2 systems, as I would like to find a universal solution that can be placed in any script, independent of what additions / hardware / OS is running.
System 1 = G4 Powerbook, running OS X 10.3.9
System 2 = 20" Intel iMac, running OS X 10.4.4

Method 1:
http://bbs.applescript.net/viewtopic.php?pid=60910

Applescript:

set Disp to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2, $4}'")

Advantages: Returns screen resolutions for dual screens
Disadvantages: Returns "" on System 2

Method 2:
http://bbs.applescript.net/viewtopic.php?id=16153

Applescript:

tell application "Finder" to set desktop_size to items -2 thru end of (get bounds of desktop's window)

Advantages: Simple
Disadvantages: Treats extended desktop as a single unit. Doesn't work on System 2 ("Can't get bounds of every window of desktop")

Method 3:
http://bbs.applescript.net/viewtopic.php?id=15425

Applescript:

do shell script " defaults read /Library/Preferences/com.apple.windowserver | grep -w -m 2 Unit"

Advantages: I wouldn't know - doesn't work on any of my systems!
Disadvantages: Is dependent upon the file com.apple.windowserver, which I don't have

Method 4:
http://bbs.applescript.net/viewtopic.php?id=10499

Applescript:

tell application "System Events" to tell process "Finder"
   repeat with i from 1 to number of windows
       if the position of window i is {0, 0} then
           return the size of window i
       end if
   end repeat
end tell

Advantages: Works a treat on System 2
Disadvantages: Only active Finder windows are detected on System 1, i.e. no window is at position {0,0}

Method 5:
http://bbs.applescript.net/viewtopic.php?id=11107

Applescript:

set {x1, y1, x2, y2} to call method "frame" of (call method "mainScreen" of class "NSScreen")
set the_resolution to {x2, y2}

Advantages: Works on both systems, but only in the context of an AS Studio application
Disadvantages: Not for simple applescript. Does not return resolution of 2nd display.

Method 6:
Jon's Commands (screen list)
http://bbs.applescript.net/viewtopic.php?id=5081
Advantages: Appears to work well for others
Disadvantages: Didn't succeed in installing it on System 1, and I would rather have a solution within the script / application, rather than one that depends on additions

Method 7:

Applescript:

tell application "DVD Player" to set Disp to viewer screen bounds

Advantages: Simple, and all systems have DVD Player installed, so is *almost* an independent solution
Disadvantages: Activates DVD Player, which is often unwanted. Also, only determines bounds of screen that viewer is on

Method 8:
http://bbs.applescript.net/viewtopic.php?pid=39892

Applescript:

tell application "System Profiler" to set Sys to system profile
set Disp to {}
repeat with p in every paragraph in Sys
   if p contains "Resolution:" then copy {(word -3 of p) as number, (word -1 of p) as number} to end of Disp
end repeat
return Disp

Advantages: Works well on System 2
Disadvantages: Doesn't work on System 1 - script hangs

Method 9:
http://bbs.applescript.net/viewtopic.php?id=8852
Objective-C, about which I know absolutely nothing, so cannot comment.

And that seems an appropriate place for me to bow out, as I have exhausted my knowledge of obtaining screen resolutions.
Have I missed anything out? Is there a better way?
Thanks in advance for any additional comments, and hope this summary helps someone.
PJ.


Filed under: Finder, System, DVD

Offline

 

#2 2006-06-28 08:15:04 am

Mikey-San
Member
Registered: 2005-07-13
Posts: 509

Re: The definitive guide to screen resolution?

Applescript:

set {x1, y1, x2, y2} to call method "frame" of (call method "mainScreen" of class "NSScreen")
set the_resolution to {x2, y2}

Advantages: Works on both systems, but only in the context of an AS Studio application
Disadvantages: Not for simple applescript. Does not return resolution of 2nd display.

NSScreen contains the ability to get all screens' frames, as well as the visible frame (frame minus menu bar and Dock) of whatever screen you wish.

+ (NSArray *)screens

and

- (NSRect)visibleFrame

Offline

 

#3 2006-06-29 08:06:29 am

PJWilson
Member
Registered: 2005-08-01
Posts: 49

Re: The definitive guide to screen resolution?

Mikey,
Thanks for that clarification on NSScreen - as it can return resolution of all screens, I think it will be my method of choice in AS applications. Now, if only I could find a universal method for simple Applescript...
As an aside, I now realise why Method1 (NSDisplaysDataType) doesn't work on System 2: System Profiler doesn't have a supported data type by this name! Opening up System Profiler, display resolution appears to be buried in a series of submenus:
Hardware -> PCI/AGP Cards -> ATY,RV360M11 -> Display
I can access the PCI profile with

Applescript:

do shell script "system_profiler SPPCIDataType"

but this only returns
"PCI/AGP Cards:

    TXN,PCIXXXX-00:
   
      Name: cardbus
      Type: cardbus
      Bus: PCI
      Slot: PC Card
      Vendor ID: 0x104c
      Device ID: 0xac56
      Revision ID: 0x0000
   
    ATY,RV360M11:
   
      Type: display
      Bus: AGP
      VRAM (Total): 64 MB
      Vendor: ATI (0x1002)
      Device ID: 0x4e50
      Revision ID: 0x0000
      ROM Revision: 113-xxxxx-134
   
        ATY,Jasper_A:
       
          Type: display
          Display Type: LCD
          VRAM (In Use): 64 MB
       
        ATY,Jasper_B:
       
          Status: No display connected"

Does anyone have any ideas how to access display resolution with system_profiler in this instance?
Thanks,
PJ.

Model: Powerbook G4
Browser: Safari 417.8
Operating System: Mac OS X (10.3.9)

Offline

 

#4 2006-12-28 01:42:45 pm

dmw
Member
Registered: 2006-12-28
Posts: 1

Re: The definitive guide to screen resolution?

This is an admittedly ugly hack, but it should work on any system with Terminal.app installed.

Applescript:


property desktopBounds : 0

tell application "Terminal"
   
   -- find out if terminal is running
   set termIsRunning to false
   
   tell application "System Events"
       set termIsRunning to ((name of processes) contains "Terminal")
   end tell
   
   if termIsRunning then
       -- open our window
       do script ""
   else
       -- launch and hide Terminal    
       run
       tell application "System Events" to set visible of process "Terminal" to false
   end if
   
   -- hide our window, zoom it, get its bounds, and close it
   set win to first window
   set visible of win to false
   set zoomed of win to true
   set desktopBounds to bounds of win
   close win
   
   -- if we started terminal, then kill it
   if termIsRunning is false then
       quit
   end if
   
end tell

desktopBounds

Advantages: gives size of desktop minus dock and menubar, doesn't require Finder to be running (e.g. for PathFinder users)
Disadvantages: Big, ugly, window flash, and requires Terminal to be installed


Filed under: terminal, System

Offline

 

#5 2006-12-28 02:46:46 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 6677
Website

Re: The definitive guide to screen resolution?

Ok, the original post a quite old, but here are two additional approaches:

First:

Applescript:

set disWidth to word 1 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep Width | cut -f 2 -d '=' | cut -f 1 -d ';'") as integer
set disHeight to word 1 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep Height | cut -f 2 -d '=' | cut -f 1 -d ';'") as integer

Second: install the little command line cscreen in /usr/bin

Here is a subroutine to get the parameters of all connected displays or of one specified display:

Applescript:

set {maxDisplay, disParam} to cscreen(0) -- 0 = all displays, >0 = specified display

on cscreen(display)
   try
       ((path to startup disk as string) & "usr:bin:cscreen") as alias
   on error
       return {false, false}
   end try
   set disParam to words of (do shell script "cscreen -l")
   set maxDisplay to item 1 of disParam as integer
   if display = 0 then
       set dispList to {}
       repeat with i from 1 to maxDisplay
           copy get_param(i, disParam) to end of dispList
       end repeat
       return {maxDisplay, dispList} -- list of max. number of displays and record(s) of parameters
   else if display is less than or equal to maxDisplay then
       return {maxDisplay, get_param(display, disParam)}
   else
       return {false, false}
   end if
end cscreen

on get_param(DIndex, d)
   set i to 14 + ((DIndex - 1) * 5)
   set DisRecord to {DNum:item i of d, DDepth:item (i + 1) of d, DWidth:item (i + 2) of d, DHeight:item (i + 3) of d, DFreq:item (i + 4) of d}
   if DFreq of DisRecord = "0" then set DFreq of DisRecord to "TFT"
   return DisRecord
end get_param

both solutions work on Panther and Tiger

Last edited by StefanK (2006-12-28 05:01:30 pm)


regards

Stefan

Offline

 

#6 2006-12-28 04:55:11 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4175

Re: The definitive guide to screen resolution?

To take the load off StefanK, I've uploaded cscreen to ScriptBuilders. I am not the author, but that's the only way I could load it.

You should be a bit wary of downloading executables from just anywhere - they can do mean things. If you want to check this copy or a copy you have received from another source, use this script after you unpack it (the hash in the script is from a known good copy I have from an independent source, and StefanK's also checks "clean" in this test):

Applescript:

property hash : "dc3620e75bf55c302322be13982f2222"
set CS to POSIX path of (choose file with prompt "Choose the unZipped cscreen file")
set tHash to last word of (do shell script "md5 " & CS)
if tHash = hash then
   display dialog CS & "'s check sum is correct" with icon 1
else
   display dialog "The file " & CS & " has been altered!" with icon 0
end if

To find out how to use it:

Applescript:

set SCR to do shell script "Posix/Path/To/cscreen -h"

and to get screen resolutions for one or more screens:

Applescript:

set Cur to do shell script "Posix/Path/To/cscreen -I"


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.2

Offline

 

#7 2006-12-28 05:03:34 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 6677
Website

Re: The definitive guide to screen resolution?

Thanks, Adam, I changed the link in my post above


regards

Stefan

Offline

 

#8 2006-12-28 07:30:10 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1787

Re: The definitive guide to screen resolution?

StefanK wrote:

Ok, the original post a quite old, but here are two additional approaches:

First:

Applescript:

set disWidth to word 1 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep Width | cut -f 2 -d '=' | cut -f 1 -d ';'") as integer
set disHeight to word 1 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep Height | cut -f 2 -d '=' | cut -f 1 -d ';'") as integer

[snip]

both solutions work on Panther and Tiger

Thanks, Stefan! I can confirm that your first method works with Jaguar as well.  smile

I've been trying to reduce it to just one shell script, but I'm not an expert. The following works on both my Jaguar and Tiger machines:

Applescript:

tell (do shell script "w=$(defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep -w 'Width'); h=$(defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep -w 'Height'); echo $w $h;") to set screenRes to {word 3 as integer, word ((count words) div 2 + 3) as integer}

It would be great if it didn't have to include two 'defaults reads', but I haven't been able to work out how to get by with just one.

The 'grep' in Tiger has a '-m' option which allows you to specify how many matches are returned. This would allow a slight simplification of the AppleScript code, but doesn't work in Jaguar:

Applescript:

tell (do shell script "w=$(defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep -wm1 'Width'); h=$(defaults read /Library/Preferences/com.apple.windowserver DisplaySets | grep -wm1 'Height'); echo $w $h;") to set screenRes to {word 3 as integer, word 6 as integer}

Last edited by Nigel Garvey (2006-12-28 07:48:49 pm)


NG

Offline

 

#9 2006-12-29 05:05:34 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1787

Re: The definitive guide to screen resolution?

Hi, Jacques.

Thanks! Those both work on both systems and are more compact than – and nearly twice as fast as – my effort. The 'grep' version may possibly be very slightly faster than the 'awk', but it's too close a call to be sure. They're still several times slower than Jon’s Commands, but since that's apparently not going to be ported for Intel machines, I'm keeping an eye on the alternatives!

Applescript:

set screenRes to screen size of beginning of (screen list starting with main screen) -- Needs Jon’s Commands.


NG

Offline

 

#10 2006-12-29 07:45:19 am

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4175

Re: The definitive guide to screen resolution?

Elegant as these solutions are, I still have to use cscreen because I have two screens. mad


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.2

Offline

 

#11 2006-12-29 08:03:04 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 6677
Website

Re: The definitive guide to screen resolution?

Adam Bell wrote:

Elegant as these solutions are, I still have to use cscreen because I have two screens. mad

No problem, Adam, this returns the values for two screens ({x1, y1, x2, y2})

Applescript:

tell (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | awk '/ Height =/||/ Width =/'") to set screenRes to {word 6 as integer, word 3 as integer, word 12 as integer, word 9 as integer}


regards

Stefan

Offline

 

#12 2006-12-29 08:15:53 am

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4175

Re: The definitive guide to screen resolution?

It does indeed, Stefan!  big_smile

Someday, I'll have to revise my Code Exchange submission on dual screens


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.2

Offline

 

#13 2006-12-29 06:55:21 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1787

Re: The definitive guide to screen resolution?

I see that the very first key in the "com.apple.windowserver" domain is "CGSInterocitorSelectMode". An interocitor, of course, is an alien device from the 1954 Sci-Fi film This Island Earth. There are quite a few people having fun with it on the Net. I quite enjoyed this effort. (Follow the "About this page" link for more information.)  wink


NG

Offline

 

#14 2006-12-29 07:42:51 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4175

Re: The definitive guide to screen resolution?

When I read the link you gave and look back at CGSInterocitor's value, mine is an integer set to zero. Is that universal or does it vary from country to country? If the latter, then it's a quite suitable name for identifying "aliens" presuming the "0" means USA.

A cursory search through Apple Docs did not find much about com.apple.windowserver, however, and nothing about CGSInterocitor, so I have no idea what it's for.


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.2

Offline

 

#15 2006-12-29 08:08:17 pm

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: The definitive guide to screen resolution?

Adam Bell wrote:

Elegant as these solutions are, I still have to use cscreen because I have two screens. mad

I saw Stefan's solution, but does this work?:

Applescript:

(do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | sed -Ee '/^ *(Height|Width)/!d' -e 's/[^[:digit:]]*//g'")'s paragraphs

Offline

 

#16 2006-12-29 08:15:44 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1787

Re: The definitive guide to screen resolution?

Adam Bell wrote:

When I read the link you gave and look back at CGSInterocitor's value, mine is an integer set to zero. Is that universal or does it vary from country to country? If the latter, then it's a quite suitable name for identifying "aliens" presuming the "0" means USA.

No. It's "0" here too – and we're not aliens.  wink


NG

Offline

 

#17 2006-12-29 08:25:13 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1787

Re: The definitive guide to screen resolution?

Qwerty Denzel wrote:

I saw Stefan's solution, but does this work?:

Applescript:

(do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | sed -Ee '/^ *(Height|Width)/!d' -e 's/[^[:digit:]]*//g'")'s paragraphs

Hi, Qwerty.

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:

Applescript:

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}


NG

Offline

 

#18 2006-12-29 08:35:30 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4175

Re: The definitive guide to screen resolution?

Nigel Garvey wrote:

Adam Bell wrote:

When I read the link you gave and look back at CGSInterocitor's value, mine is an integer set to zero. Is that universal or does it vary from country to country? If the latter, then it's a quite suitable name for identifying "aliens" presuming the "0" means USA.

No. It's "0" here too – and we're not aliens.  wink

Apparently Canadians aren't either (I'm happy to say) lol


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.2

Offline

 

#19 2006-12-30 05:20:17 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 1787

Re: The definitive guide to screen resolution?

Adam Bell wrote:

Nigel Garvey wrote:

Adam Bell wrote:

When I read the link you gave and look back at CGSInterocitor's value, mine is an integer set to zero. Is that universal or does it vary from country to country? If the latter, then it's a quite suitable name for identifying "aliens" presuming the "0" means USA.

No. It's "0" here too – and we're not aliens.  wink

Apparently Canadians aren't either (I'm happy to say) lol

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! yikes


NG

Offline

 

#20 2006-12-30 10:05:37 am

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4175

Re: The definitive guide to screen resolution?

big_smilelollol


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.2

Offline

 

#21 2007-01-02 03:13:06 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: The definitive guide to screen resolution?

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.

Applescript:

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.

Last edited by Qwerty Denzel (2007-01-02 04:09:59 am)

Offline

 

#22 2007-01-02 03:41:16 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 6677
Website

Re: The definitive guide to screen resolution?

Qwerty Denzel wrote:

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.
I presume that, as the last one died of an obscure error, this too will fail on Jaguar.

Hi Qwerty,

starting the script with two displays I get an runtime error (Expected “, ” or “}” but found “{”.)


regards

Stefan

Offline

 

#23 2007-01-02 04:01:04 am

Eelco Houwink
Member
Registered: 2002-11-19
Posts: 310

Re: The definitive guide to screen resolution?

FWIW, Extra Suites may come in handy when changing screenresolutions AND depth on a fly (here for single screen):

Applescript:


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

Last edited by Eelco Houwink (2007-01-02 04:04:56 am)


Eelco Houwink

Filed under: System, extra

Offline

 

#24 2007-01-02 05:58:03 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: The definitive guide to screen resolution?

Stefan, I've updated the script, so hopefully it now works for you.

Offline

 

#25 2007-01-02 06:04:29 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 6677
Website

Re: The definitive guide to screen resolution?

Qwerty Denzel wrote:

Stefan, I've updated the script, so hopefully it now works for you.

yes, it works perfectly now with two displays


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

[ Generated in 1.248 seconds, 9 queries executed ]

RSS (new topics) RSS (active topics)