Is It Possible to Center a Window Horizontally and Maintain Its Current Vertical Position?

I've looked at some other forum discussions re: centering windows, but haven't yet figured out the solution for this particular, desired action. The Center and Center at actions don't seem to provide the necessary parameters (but I'd be delighted to be wrong, if they do).

I want this command to work for windows of any size, and I don't want to specify a vertical position; I want that to remain unchanged (wherever that window happens to be located vertically).

Any suggestions would be appreciated.

Thanks.

1 Like

You can use the Manipulate a Window action to move the window to:

SCREEN(Front,MidX)-WINDOW(Width)/2
WINDOW(Top)

image

2 Likes

Thank you!! :blush:

Thanks for sharing this formula. I wasn't aware of this function and just tried to implement it into my Window moving Macros.

Unfortunately in my case it seems that this calculation takes more time than the resizement of the window. As follows the screenshot of my macro:

The second action (currently disabled for testing purposes) works with just one press of the hot key. If I use the first action like shown I have to press the hot key combination twice to move the window into the middle of the screen.

Is there something I can do against this behaviour?

Thanks in advance.

The calculation will take essentially zero time.

The Accessibility API occasionally bounces the window changes, and this can be exacerbated if any part of the window will move off the screen or by some software on your Mac if it turns on certain accessibility features.

Check the values of the two calculations:

SCREEN(Front,MidX)-WINDOW(Width)/2

and

SCREENVISIBLE(Main,MidX)*30%

and see what their difference is. Even a subtle difference may be responsible for the change in behaviour.

The speed of the calculation will not be.

You can try simply repeating the action, or repeating the action with a small pause between it.

Note that the second equation calculation does not really make sense:

SCREENVISIBLE(Main,MidX)*30%

This will be 30% of the distance between the desktop left edge and the middle of the main screen - if you have two monitors, this will not necessarily make any sense, although the desktop

If you want 15% across the main screen, you can use:

SCREENVISIBLE(Main,Left) + 15%*SCREENVISIBLE(Main,Width)

and Keyboard Maestro has a built in simplification for this which is:

SCREENVISIBLE(Main,Left,15%)

2 Likes

Thanks so much Peter!

It took me a while and some tests but at the end your hints helped me to find the reason for my issue.

Indeed this…

… is something I probably never will get. Since both are just calculations I probably will never understand why one would lead to a different behaviour than the other.

But anyway, first of all as I copied your example into my action I didn't recognize that there was the difference between "SCREEN" and "SCREENVISIBLE". Apologies for that.

The issue in my case was that the first calculation is - of course - based on the current window size.

This means if I want to position a window in the middle of the screen and resize it your formula just works if the window resizement happens before the positioning.

Thanks again!

1 Like

You don't actually need to -- you could include the resizing in the calculation. So something like (totally untested):

SCREENVISIBLE(Main,MidX) - (WINDOW(Width) * 70%) / 2

The best way is whichever one you'll understand when you revisit the macro in a year's time!

3 Likes

Thanks a lot @Nige_S!

I just checked and it behaves the same. I need more than one hotkey press to gain position and size.

If the window size doesn't change it works with one press.

I will just stuck with my two action version which works fine. :sunglasses:

Thanks again.

Hey Guys,

It looks to me like the most effective solution to this problem is to move the window first, so it has plenty of room to expand and then resize it and move it again.

The system can get picky about how it allows you to resize a window with automation, and that's why I always prefer to resize windows of scriptable apps with AppleScript using window-bounds. But that solution is not always available.

-Chris

Move and Resize Front Window v1.00.kmmacros (7.3 KB)
Keyboard Maestro Export

4 Likes

@ccstone's version, separating the move and resize, might sort things out for you. If not, the other way is to set a variable to your new, desired, window bounds and then repeat the move/resize until actual matches desired.

Chris's method is much shorter and easier :wink:

2 Likes