views:

995

answers:

1

I'm looking into windows management on OS X (trying to achieve something like WinSplit Revolution), and I need to use applescript to pull out the maximum size of a window on a given monitor. Currently I've found:

tell application "Safari"
    set screen_width to (do JavaScript "screen.availWidth" in document 1)
    set screen_height to (do JavaScript "screen.availHeight" in document 1)
end tell

This works for the main monitor on a multiple monitor setup, but doesn't provide at all for secondary monitors. I've also read into the method detailed here, and this obviously doesn't work for multiple displays in an efficient manner. Is there an effective way to get the maximum window size of multiple displays with applescript?

A: 

There have been many attempts to get monitor dimensions by reading system plist files. See http://macscripter.net/viewtopic.php?id=15425

If you have access to AppleScript studio (ASS) terms, you can make method calls into NSScreen to get all the monitors then ask them for their sizes. The easiest way to use ASS terms in a plain AppleScript is to compile an empty ASS application in Xcode. In this example, I've created a simple ASS app name ASSAccess which gives me access to ASS terms:

tell application "ASSAccess"
    -- The first item in this list will be your main monitor
    set screensArray to call method "screens" of class "NSScreen"

    set Displays to {}
    if {} is not screensArray then
     repeat with displayNo from 1 to (count screensArray)
                    -- call visibleFrame instead to take into account the Dock and menubar
      set dims to call method "frame" of (item displayNo of screensArray)
      copy dims to end of Displays
     end repeat
    end if
end tell

Now, the issue I run into with these dimensions is the coordinate system. NSScreen's frame method gives you a rectangle with the origin in the LOWER left hand corner of the main monitor. Then, any secondary screens are given relative to that origin. If you are trying to determine if a window is within these bounds, window position is giving within a coordinate system with the origin at the UPPER left hand corner. This is a whole mess of conversion that I haven't figure out yet.