Tuesday, November 19, 2019

#1 2012-01-23 05:00:05 am

hagimeno
Member
Registered: 2009-08-06
Posts: 62

Center Window Horizontally and Vertically

Hi,

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

Hagi


Filed under: window, center, vertically

Offline

 

#2 2012-01-23 09:26:34 am

SuperMacGuy
Member
From:: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: Center Window Horizontally and Vertically

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.


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#3 2012-01-23 11:25:39 am

rdelmar
Member
From:: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Center Window Horizontally and Vertically

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.

Ric

Offline

 

#4 2012-01-23 12:43:07 pm

hagimeno
Member
Registered: 2009-08-06
Posts: 62

Re: Center Window Horizontally and Vertically

Hi,

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?

Hagi

Offline

 

#5 2012-01-23 01:23:14 pm

rdelmar
Member
From:: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Center Window Horizontally and Vertically

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.

Ric

Offline

 

#6 2012-01-23 01:30:09 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11620
Website

Re: Center Window Horizontally and Vertically

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

Applescript:


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

Applescript:


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

Last edited by StefanK (2012-01-23 01:33:49 pm)


regards

Stefan

Offline

 

#7 2019-05-25 09:34:37 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 23

Re: Center Window Horizontally and Vertically

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 Lynda.com. So I have just enough knowledge to know I am stupid!

Thanks!

Chris

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

Offline

 

#8 2019-05-26 12:30:02 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6034

Re: Center Window Horizontally and Vertically

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:

Applescript:

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}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2019-05-26 02:00:17 am

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 23

Re: Center Window Horizontally and Vertically

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.

Offline

 

#10 2019-05-26 02:05:39 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6034

Re: Center Window Horizontally and Vertically

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2019-05-26 12:20:58 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 23

Re: Center Window Horizontally and Vertically

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!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)