An AppleScript to Center the Frontmost Window

32” is the size of my Samsung U32J59x, which at the time of purchase, was a great deal and remains perfect for my usage. I’ve used 32” and 27” displays, and while I would probably choose 32” over 27” when picking out another display, 32” does hit the edge of usability for me. There are just times when a window is over to the left of center on the display, and I really can’t see it that well (I do run my text fairly small on the Mac).

For the record, I had a 27” Dell 4k display at my last gig, and I have a 27” Samsung that I grabbed from Walmart around Christmas time to plug into my MacBook Air. 27” is a great size but I really like having the 32” at home.

Anyway, when I log into my Mac at home and a window is up in a corner somewhere, I’m tempted by the urge to center it without having to mouse up to the title bar and move it manually. It seems like something that I should be able to keyboard shortcut.

AppleScript is an obvious and easy way to cobble a solution together. I checked in with ChatGPT and it offered me a decent starting point; I do get better results specifying the resolution of the display, although I suspect I’ll be able to modify it to dynamically check the dimensions of the display so that this will work on a laptop, too.

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
    tell application process frontApp
        if (count of windows) > 0 then
            set win to front window
            set winPos to position of win
            set winSize to size of win
            set winWidth to item 1 of winSize
            set winHeight to item 2 of winSize

            -- Set your screen resolution here (adjust if needed)
            set screenWidth to 3008
            set screenHeight to 1692

            set newX to (screenWidth - winWidth) / 2
            set newY to (screenHeight - winHeight) / 2

            set position of win to {newX, newY}
        end if
    end tell
end tell

Window Management and a Large Display

A funny by-product of being a laptop user for many years for me was that I really never worked with multiple windows on my Mac. I was a PowerBook (and then MacBook) user for over a decade before I introduced a desktop computer back into my setup. My laptop usage effectively looked like full-screen (but not zoomed) windows, and I’d command-tab between active applications. On a 13” Air or 14” MacBook Pro, this makes a lot of sense. But on a big display, there’s really no reason to run Safari that big (Logic or iMovie, sure).

Here’s today’s ham recipe on my laptop’s built-in display, centered using macOS’s Window > Center command:

Centered Window on MacBook Air 13”
Centered Window on MacBook Air 13”

There’s a fair bit of white space, owing to the webpage’s design, but it’s readable and doesn’t really ask for resizing.

Here’s the same page on my Samsung display:

Safair Window Centered on my Samsung 32” Display
Safair Window Centered on my Samsung 32” Display

In this way, a smaller MacBook ends up being used, for me, a lot like an iPad: pretty much full screen in every app. It’s a lot of fun to be able to plug into a larger display and have a different experience with windowing, and I can totally see why multiple monitors or large ones really do improve productivity. There is a cognitive cost to all of that context switching.