Hi GG.
I’ve only ever had one screen attached to any of my computers, so I wrote the handler in the hope that it would be useful to those with more than one!
It returns a list of records, each record corresponding to a screen and having screenID and usableBounds properties. With my one screen, there’s only one record and I don’t need to know the screen’s ID. I can simply get its usable bounds like this:
set screensInfo to getUsableScreenBounds()
set usableBounds to usableBounds of item 1 of screensInfo
--> {0, 23, 2560, 1440} for my screen.
With more than one screen, you’d need to know or be able to find out the ID(s) of the screen(s) of interest and loop through the list to find the relevant record(s). Obviously the exact form of the search and what it returned would depend on how many screens’ bounds you needed to know, but for a particular screen:
set myScreenID to 69981780 -- Or whatever. You'd need to know this or find it out.
set screensInfo to getUsableScreenBounds()
repeat with thisScreen in screensInfo
if (thisScreen's screenID is myScreenID) then
set usableBounds to thisScreen's usableBounds
exit repeat
end if
end repeat
usableBounds
--> {0, 23, 2560, 1440} for my screen.
It’s possible, with a different handler, to get the information specifically for the “main” screen, which is described in the Xcode documentation as “the screen object containing the window with the keyboard focus.”
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
on getMainScreenUsableBounds()
set |⌘| to current application
set mainScreen to |⌘|'s class "NSScreen"'s mainScreen()
set wholeFrame to mainScreen's frame()
set visibleFrame to mainScreen's visibleFrame()
-- In High Sierra, the frame rects are returned as lists instead of records, but the functions below work with either.
set x1 to (|⌘|'s NSMinX(visibleFrame)) as integer
-- The y origin in NSScreen is at the bottom of the screen. It needs to be converted to an origin at the top for AS.
set y1 to ((|⌘|'s NSMaxY(wholeFrame)) - (|⌘|'s NSMaxY(visibleFrame))) as integer
set x2 to (|⌘|'s NSMaxX(visibleFrame)) as integer
set y2 to (y1 + (|⌘|'s NSHeight(visibleFrame))) as integer
return {x1, y1, x2, y2}
end getMainScreenUsableBounds
getMainScreenUsableBounds()
--> {0, 23, 2560, 1440}