Question

I'm creating a few screen casts and wanted to exactly center my windows in my screen. Any good way to do this on the Mac? I found an Apple Script example (and a few linked screens) but none of them support centering 'Terminal' vertically (although they do support centering other applications vertically).

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

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.

An alternative is MercuryMover. 'Center window' is one of the built-in shortcuts.

You can move windows around with shortcuts, define custom positions - and do some more. Hint: try the preference file to define custom pixel numbers other than 1, 10, 100.

It is expensive with $20, but pays over time. Anyway, there's an old-school trial… MacUpdate: MercuryMover

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top