Friday, July 30, 2010

#1 2005-02-14 12:23:24 pm

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

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?


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.4

Offline

 

#2 2005-02-14 03:48:42 pm

Julio
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

Re: Getting Screen Coordinates

These are the items in my library:

Applescript:

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

Applescript:

{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}

Applescript:

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


Filed under: System

Offline

 

#3 2005-02-14 06:44:44 pm

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

Re: Getting Screen Coordinates

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:

Applescript:

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 <key>):
<key>Width</key>LFtttt<integer>1152</integer> 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:

Applescript:


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.


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.4

Filed under: System

Offline

 

#4 2005-02-15 04:17:14 am

Julio
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

Re: Getting Screen Coordinates

I forgot also these, but they require scripting additions:

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.194 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)