Center and resize window with AppleScript

by Martin on 9/01/2009

I use a 15″ MacBook Pro that I carry between work and home. At work I have a 30″ Cinema Display which means I can actually have half of the Flash IDE showing at once. Mostly it’s plug and play but every once in a while some app just doesn’t realize that I’ve unplugged the huge display and some window ends up at some awkward position where I have to drag it out and resize it, or even worse; It ends up outside the boundaries of the laptop screen altogether.

AppleScript to the rescue. Copy this script and save it into your AppleScripts folder. You can assign it to a Quicksilver hotkey if you’d like, but I find it’s convenient enough to just launch it from Quicksilver in the usual manner.

What it does is simply resizing the window and centering it on the display. There are more elegant ways to do this, but this one works even with apps that have little or no support for AppleScript at all. You can adjust the lines:

1
2
  set position to {220, 50}
   set size to {1000, 800}

to fit your screen real estate as those values are set to center on a 1440 / 900 display.

1
2
3
4
5
6
7
8
9
10
tell application "System Events"
  set frontmostProcess to name of first item of ¬
  (processes whose frontmost is true)
  tell process frontmostProcess
          tell window 1
              set position to {100, 0}
              set size to {1200, 800}
        end tell
    end tell
end tell

No comments yet.

Write a comment: