Center Window Horizontally and Vertically


somebody can help me to center a window in both Hor and Ver?
mainWin’s |center|() do the jb but only horizontally


I’ve always found it to work vertically, as in the way you’d expect “center” to work. Sometimes it seems the centering is nudged just a bit to the top, maybe to account for the dock.

The docs on center say: “The window is placed exactly in the center horizontally and somewhat above center vertically”. I would call it more than “somewhat” – it’s considerably above center in my experience, even though I have my dock on the side, so I don’t think the dock is what’s causing the above center “centering”. If you want it more centered just choose the “Center Horizontally” and “Center Vertically” options in the size inspector in IB.



I know that is possible to center in IB but this means that window of compiled app will open exactly in the center (H & V) of different monitors of different users?


I don’t know the answer to that question. If it doesn’t work, you can aways use the visibleFrame method of NSScreen to get the screen size and do the centering manually.


Just for fun, this is the common math to center a window.
the variable theWindow is the NSWindow instance to be centered

set screenFrame to current application's NSScreen's mainScreen()'s frame()
set {|width|:screenFrameWidth, |height|:screenFrameHeight} to |size| of (screenFrame as record)
set {|x|:screenFrameX, |y|:screenFrameY} to |origin| of (screenFrame as record)
set {|width|:windowWidth, |height|:windowHeight} to |size| of (theWindow's frame() as record)
set midX to screenFrameX + screenFrameWidth / 2.0
set midY to screenFrameY + screenFrameHeight / 2.0
set newOrigin to {origin:{|x|:midX - windowWidth / 2.0, |y|:midY - windowHeight / 2.0}}
theWindow's setFrameOrigin_(newOrigin)

PS: it looks clearer in ObjC

NSRect screenFrame = [[NSScreen mainScreen] frame];
NSSize windowSize = [theWindow frame].size;
[theWindow setFrameOrigin:NSMakePoint((NSMidX(screenFrame) - windowSize.width / 2), (NSMidY(screenFrame) - windowSize.height / 2))];

I realize this thread is 7 years old, but I am trying to solve the same problem in 2019, and tried Stefan’s AppleScriptObJC code and Script Debugger is coming back with an error on the second line of code saying “Can’t make {{0.0, 0.0}, {2560.0, 1440.0}} into type record.”

I tried changing “record” to “list” but that gives me an error for the second line of code “Can’t get |size| of {{0.0, 0.0}, {2560.0, 1440.0}}.”

I am trying to hack together some AppleScriptObJC, but I am a complete noob for the most part (I have bought Shane’s book and skimmed it, and watched about half of an introduction to Objective-C on So I have just enough knowledge to know I am stupid!



Model: 2015 27" iMac i7
AppleScript: 2.4
Browser: Safari 605.1.15
Operating System: macOS 10.13

There’s been a bug in the past couple of versions of the OS where NSRects are returned as lists of lists, rather than records. This variation on Stefan’s code should work regardless:

set screenFrame to current application's NSScreen's mainScreen()'s frame()
set midX to current application's NSMidX(screenFrame)
set midY to current application's NSMidY(screenFrame)
set windowFrame to theWindow's frame()
set windowWidth to current application's NSWidth(windowFrame)
set windowHeight to current application's NSHeight(windowFrame)
theWindow's setFrameOrigin:{midX - windowWidth / 2.0, midY - windowHeight / 2.0}

Thanks Shane!

When I run that script SD tells me that the variable “the Window” is not defined.

I tried several obvious things to work it out myself, but no luck.

Like the previous examples, it assumes you already have a reference to the window you’re trying to place. It’s meant for use in Xcode projects, or scripts where you’re using code to create the window – not for placing any old window.

Thanks Shane,

I am trying to set the windows position before I display it. I was able to figure out how to do it. Thanks so much for the help!