Center Windows in Interface Builder

December 6th, 2005

How many times have you been bitten by the problem of moving a window in Interface Builder, for easier editing, and then neglecting to put it back before saving it? Sometimes this can pop up as an embarrassing bug when you realize that your preferences have been saving an appropriate location for the window, but now new users are seeing it pop-up in the lower left corner of the window. Where you left it the last time you edited it in Interface Builder!

One of the ingredients leading to this disaster is the relative difficulty of getting the window back to precisely where it belongs. Many times, a window is saved “centered” with the window autoposition springs set so that it will be centered on whatever screen the user has. But the tedium of getting it “back to center” can make you hesitant to move it to a more convenient position for editing. Worse, it can make it easy to “postpone” the act of putting it back where it belongs until you forget about it and ship that way.

Unfortunately, the amazingly useful alignment and centering tools provided in Interface Builder only work on view-level interface elements. They don’t work on windows. Wouldn’t it be nice if you could easily and quickly snap a window back to a centered position?

My Center Window script lets you do just that. By adding this to the Interface Builder scripts menu, you can easily bounce a window back to horizontal center at any time by selecting the script from the Apple Script Menu, or (better!) by pressing a FastScripts keyboard shortcut. I’m using Cmd-Opt-C to center windows in the blink of an eye.

Note: This script requires the Jon’s Commands scripting addition. If you don’t want to install that, you can modify the script to define the width and height of your main screen manually.

It should be pretty easy to modify the script to also position the Y-axis as you see fit.

6 Responses to “Center Windows in Interface Builder”

  1. Brian Webster Says:

    A couple versions of IB ago, Apple added a little “Lock” checkbox to the size inspector for windows. If you check the box, it will lock the coordinates of the window to its current location, so you can move the window around in IB with abandon and it will still be saved in the nib file with the saved coordinates. This problem used to drive me nuts too until I discovered that. They still don’t have a center window command though. :-)

  2. Daniel Jalkut Says:

    Wow, that’s a great feature. I will have to look into using that. Should make this script much less necessary, but at least the script is already written :)

  3. Michael Tsai Says:

    Shouldn’t you center it on the user’s screen, which might be a different size/shape?

  4. Daniel Jalkut Says:

    Michael: For windows managed in this way, I assume the use of the “auto position” window attributes. From the “Size” pane of a window inspector in IB, you can see the Auto Position springs can be set so that a window will autoposition to the center of the user’s screen.

  5. Michael Tsai Says:

    Aha, I forgot that windows had auto-position springs, too. :-)

  6. Shine Says:

    Yes, I agreed with what Daniel Jalkut said. Auto Position springs can be set so that a window will autoposition to the center of the user’s screen. But thanks to the author of this post.

Comments are closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.