Center Window on Screen

Solution 1:

I enjoy using SizeUp to manage windows, one of which is a center on screen option. Good luck.

Solution 2:

Not sure if this might do what you are looking for as well, but Divvy is also a neat piece of software for arranging windows.

I suggest you watch the screencast on their website to see if it does what you are looking for, but basically it has a grid where you can change the size and position of the frontmost window.

You can also change the grid it displays to have finer control over the size of the windows you are working with.

Hope this helps.

Solution 3:

Kevin. I’m the author of the post that you linked to on incrementalism.net.

The reason the Terminal window moves to the top is just a bug in Terminal’s AppleScript support.

This version does the vertical centering and works around the Terminal bug:

tell application "Finder"
    set screenSize to bounds of window of desktop
    set screenWidth to item 3 of screenSize
    set screenHeight to item 4 of screenSize
end tell

tell application "System Events"
    set myFrontMost to name of first item of ¬
        (processes whose frontmost is true)
end tell

try
    tell application myFrontMost
        set windowSize to bounds of window 1
        set windowXl to item 1 of windowSize
        set windowYt to item 2 of windowSize
        set windowXr to item 3 of windowSize
        set windowYb to item 4 of windowSize

        set windowWidth to windowXr - windowXl
        set windowHeight to windowYb - windowYt

        if myFrontMost is "Terminal" then
            set bounds of window 1 to {¬
                round ((screenWidth - windowWidth) / 2) rounding as taught in school, ¬
                round ((screenHeight + windowHeight) / 2) rounding as taught in school, ¬
                round ((screenWidth + windowWidth) / 2) rounding as taught in school, ¬
                round ((screenHeight + windowHeight) / 2 + windowHeight) rounding as taught in school}
        else
            set bounds of window 1 to {¬
                round ((screenWidth - windowWidth) / 2) rounding as taught in school, ¬
                round ((screenHeight - windowHeight) / 2) rounding as taught in school, ¬
                round ((screenWidth + windowWidth) / 2) rounding as taught in school, ¬
                round ((screenHeight + windowHeight) / 2) rounding as taught in school}
        end if

        set the result to bounds of window 1

    end tell
end try

I hope that helps, if you haven’t already paid for one of the other options. I also added a comment with this workaround to the original post.

Solution 4:

Fixes for:

  • The bug with Terminal
  • Weird process names (firefox-bin)
  • Sizes of menu bar and Dock
  • Applescript support disabled in Preview
  • Windows that are almost full width / height will be resized to full width / height

Remarks:

  • rounding as taught in school isn't really needed
    • the default is rounding to nearest, which rounds .5 to the nearest even integer (e.g 22.5 to 22)
  • Really, mixedCase variable names even here?

Still, there's a dozen more things that could go wrong. The Dock part isn't really needed if you have hiding always turned on. (But if for example orientation = bottom, you might want to set dth to dth - 4.)

-- defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool yes

tell app "Finder" to set {0, 0, dtw, dth} to bounds of window of desktop
set dtw0 to dtw
set dth0 to dth

tell app "System Events" to tell dock preferences
    set hiding to autohide
    set edge to screen edge as string
end tell

if hiding then
    set docksize to 4
else
    tell app "System Events" to tell process "Dock"
        set sz to size in list 1
        if edge = "bottom" then
            set docksize to item 2 of sz
        else
            set docksize to item 1 of sz
        end if
    end tell
end if

if edge = "bottom" then
    set dth to dth - docksize
else if edge = "right" then
    set dtw to dtw - docksize
else if edge = "left" then
    set dtw to dtw + docksize
end if

set a to (path to frontmost application as text)
try
    tell app a
        set b to bounds of window 1
        set w to (item 3 of b) - (item 1 of b)
        set h to (item 4 of b) - (item 2 of b)
        if w > dtw0 - 40 then set w to dtw
        if h > dth0 - 40 then set h to dth
        set item 1 of b to {dtw - w} / 2
        set item 1 of b to (dtw - w) / 2
        set item 2 of b to (dth - h + 22) / 2
        set item 3 of b to (dtw + w) / 2
        set item 4 of b to (dth + h + 22) / 2
    end tell
    if app "Terminal" is frontmost then
        tell app "Terminal" to set position of window 1 to b
    else
        tell app a to set bounds of window 1 to b
    end if
end try

Solution 5:

If you want to exactly center them (so horizontally and vertically) then you can use the example given in the comments on the site you linked to, and adapt the answer posted to this stackoverflow question.

I ended up with this:

tell application "System Events" to tell application "Finder"
    set {posx, posy, screenWidth, screenHeight} to bounds of window of desktop
end tell

tell application "Terminal" to set {windowLeft, windowTop, windowRight, windowBottom} to bounds of window 1

set windowWidth to windowRight - windowLeft
set windowHeight to windowBottom - windowTop

set newBounds to {¬
    ((screenWidth - windowWidth) / 2), ¬
    ((screenHeight - windowHeight) / 2), ¬
    ((screenWidth + windowWidth) / 2), ¬
    ((screenHeight + windowHeight) / 2) ¬
        }

set newPosition to {¬
    ((screenWidth - windowWidth) / 2), ¬
    ((screenHeight - windowHeight) / 2) ¬
        }

tell application "Terminal" to set bounds of window 1 to newBounds
tell application "Terminal" to set position of window 1 to newPosition

Keep in mind that this will center window 1, so if you happen to use tools like Visor, you will have to use window 2 instead.