How Do I Get the Bounds (frame) of the Main Display (Screen)?

foundation
asobjc

(Jim Underwood) #1

If I have multiple monitors attached to my Mac, how do I get the bounds of the main display (screen)? This would be the internal display.

If I want to move a window from another screen to the main screen, I need to know its bounds. I read somewhere that ASObjC code had been published for this, but I can’t find it.


Weird things in Finder's "explore"
(Shane Stanley) #2

Use this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theFrame to current application's NSScreen's mainScreen()'s frame()

You can also use visibleFrame() instead of frame() if you want to exclude the menubar and Dock.

Note that the main screen is the one with the active window, and may not be the primary screen, which you can get the bounds of like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theFrame to current application's NSScreen's screens()'s firstObject()'s frame()

(Jim Underwood) #3

Thanks, Shane. That is perfect – just what I needed. :smile: