Hi, this is Gary with MacMost.com. Today I'm going to show you how to
perfectly center a window on your Mac using a script.
MacMost is brought to you thanks to a great group of more than 800 supporters.
Go to MacMost.com/patreon. There you can read more about the Patreon Campaign.
Join us and get exclusive content and course discounts.
So some of the most popular system extensions for the Mac are programs that will allow you to
position and organize the windows on your screen. But I like to do things without
third party apps whenever possible. You can use scripting to position windows and do things like
center them. As a matter of fact centering a window is something I often want to do
and I've been asked recently by a few people how you can perfectly
center a window on your screen. So here's a script that will do that.
For instance here I have the Reminders app and let's say I want this to be perfectly in the
center. I can get it close by just moving it there. But let's say I want to use the
keyboard shortcut and it just snaps right to the middle of the screen. So we're going to do this
in Automator. In Automator we're going to create a Quick Action. Then the Quick Action is just
going to have one thing in it. That's going to be Run JavaScript. So right here, where it says your
script goes here, that's where we're going to start with our script. The first line is simply
going to get the current application. So whatever app you're using. The frontmost window of the
list of windows is the one that's first. Position zero. We'll store that in the variable top window.
Next we'll get the boundaries for that window. So, topWindow.bounds and we'll store that in
windowBounds. Now let's actually return that here doing nothing else. Just to test it out.
So if I Run and then I look at the Results I could see that this window is at position 100,
59 (x and y position) and it's 997 wide and 610 high. It's referring to
the frontmost window of the app we're using. So the Automator window itself.
Next let's get the size of the screen. We can do that looking at the Application Finder,
the desktop and the window of the desktop. So in other words this background here is treated
as a window. We can get the bounds of that, in other words the total size of this screen. That
will also be stored in a variable with an x, y, width, and height. So now from that information
we can calculate the exact position to put the window. First thing we want to do is figure out
the x position. To do that we're going to take the width of the screen, we're going to subtract
the width of the window. That gives us the remaining space to the left and right side.
Divide that by 2 because we want half of that to be on the left side of the window and half
to be on the right. So if we set the x position of that window so it's exactly at this location.
Then the window in its current size will be centered horizontally.
Now we'll do the same thing but with the vertical position. So we'll take the height of the desktop.
Subtract the height of the window. Divide that by 2. Set the vertical position to that exact number.
Now all we need to do instead of returning a value, we don't need to return anything,
we just need to set the boundaries of that window to our new values. So the width and
height are the same. We've only changed the x and the y value. We reposition the window to
be at a spot where it's exactly centered. Now let's try that here. If I run it should act on
this current window. The Automator window. So you can see it does. It centers this window.
Now we want this to work as a Service in any app. So we're going to have to make some changes here
because we're getting the top window of the current application. But when we run this as
a regular script in the Services menu this isn't going to work because the current application is
technically the script that is running. Not the window you're using. So we're going to have to
replace this here to get the top window in another way. To do that we're going to use this code here.
So these three lines first are going to get the name of the frontmost app. So it's going to look
at System Events. All the processes running and do a search but only return the app where frontmost
is true. Then it's going to get the name of that app. So the name of the frontmost
app. Then it's going to go and say great, the front app is that application with that name.
Then we can get top window or the front app. So this will work even if we're not
running this in Automator. But it should work in Automator as well. As a matter of
fact if we move this window out of the way here, Run, we can see it works just fine.
Notice I've save this file now and I've called it Center Window. So that's how it's going to
appear in the Services menu. We'll try using it here in Reminders. So we'll go to Reminders,
Services, and then Center Window. Now it's going to warn us about permissions. It's going to say
that System Events wants to control this. We'll say OK. After we've given it permission you could
see it works. Now we don't need to do that again. So, Reminders here, we go to Services,
Center Window it'll just do it. But it will ask for permission the next time we want to use it
in a different app. So in the Contacts app we go to Services, Center Window and then we have to
give permission again. Now it will work in the Contacts app from that point on. We can check
those Permissions by going to System Preferences and then to Security & Privacy, Privacy.
We'll see under Automation the apps there. So there's SystemEvents for Contacts and there's
SystemEvents for Reminders. So these are added there each time that we run it in a new app.
So now what we need to do is assign this a keyboard shortcut. So in System Preferences
again we'll go to Keyboard. We'll go to Shortcuts. Go to App Shortcuts. Then we'll add one here and
we need to set it to All Applications. The menu title is exactly what's seen under Services here.
Center Window. So we type Center Window. For the keyboard shortcut I'm going to Control Command and
backslash. Add. So now let's move this here. We go into Reminders Services we can actually see that
keyboard shortcut there now. In fact I can just use it to quickly center the Reminders window.
Now there are tons of variations on this. For instance, let's say you want to center the window
just horizontally but have it snap to the top of the screen. Well, just set the y value instead of
that to zero. Now if I Save when I use the keyboard shortcut you could see it
does exactly that. So I could have saved this as Center Top Window and have a different keyboard
shortcut for it. If I want to snap the window to the top right then instead of dividing by 2
I would just use the full value of the extra space. So all the extra spaces on the left.
Now when I save that and use the keyboard shortcut you can see it snaps it to the
top right. Let's say I wanted it to be just the right half of the screen and snap to the upper
right hand corner but not changing the height. So what I could do is take the window bounds width
and set that to the desktop bounds but divided by 2 which is the half of the width of the screen.
Then the x value I could also set to there. So setting the width to half of the screen and the
x by half of the screen that should actually put the left side of the window right in the middle
and extending half of the screen. So I'll Save that. Use the keyboard shortcut and you could see
it puts it horizontally in the right half of the window. It doesn't do anything with the height.
So there are tons of ways to customize these using the simple math here to get the window to go to
any position you really want. Also keep in mind you could hard code values in here. This is really
useful if you have more than one screen. So you could have a second screen attached to your Mac.
Then the desktop bounds are going to be very different and it's going to be hard
to figure out the center of the actual current screen. So you may want to actually set these
to specific values. Like here I know that my main screen is 1280, 720 so now I can
run this and it will center perfectly regardless of whether or not I have extra screens attached
to my Mac. So if you find that desktop bounds isn't useful because you have multiple screens
you could just use hard coded numbers in here. That will work just for you on your current setup.