Move and resize app windows via AppleScript

May 28, '08 07:30:00AM

Contributed by: philostein

I'm a neatness freak, and I hate lots of messy overlapping windows everywhere. But I'm also a keyboard junkie, and I hate using the trackpad a lot. And I don't want anymore background processes hogging memory, or more applications to open for such a simple task -- so what to do? Answer: AppleScript. I wrote a collection of AppleScripts that use System Events to control the window size and position of the frontmost application. Also, the window snaps to a grid to make it easy to place next to other windows without overlapping.

You can find the scripts on my iDisk public folder (Go » iDisk » Other User's Public Folder, then enter phildooher). Download the zip file and put the (nine) scripts in your usual script folder. The scripts are known to work on OS X 10.4.11, but are untested on 10.5. [robg adds: I tested a few of these, but not all, on 10.5, and they seem to work as described.]

Assign keyboard shortcuts to these scripts (using Quicksilver or your other favorite tool), and you can move and resize many application windows without using the mouse. Also, there is a 'Finder window 1 remember position' script to stop those pesky Finder windows wandering about after closing. These scripts work in many apps, including at least these: Finder, Safari, Mail, Firefox, TextEdit, Script Editor, Preview, OpenOffice Aqua, Skype, and many others. They don'e seem to work in iTunes, NeoOffice (window 1 resizes, but no scroll bars), and Word 2004. Apps that require specific window sizes, or have floating windows, can be uncooperative. QuickTime Player usually resizes windows proportionally, so video playback might fit to the window instead of being the correct aspect ratio. There is more info on how the scripts work inside the scripts (especially the 'Frontmost application window snap to grid move left' script).

I have a 13" MacBook, so the screen size for the scripts is set to 1280x800 (my Dock is on the right, hidden.) Change the _screenHeight and _screenWidth variables if your size is different (or if you don't want windows behind the Dock). The grid size I have chosen is 64 horizontally and 50 vertically (64 fits into 1280 twenty times, and 50 fits into 800 sixteen times.) Change the variable _gridSize if you prefer a different grid (ie minimum movement size).

The scripts cause the window to snap to a grid before actually being moved. Therefore, some windows will move slightly the opposite way than intended at first. The 'Finder window 1 remember position' script works by turning the window toolbar off and on. You can also stick the window by clicking on the toolbar with the mouse (but hey -- that's cheating). I use keyboard shortcuts that are (for me) easy to remember:

Check your apps to make sure these shortcuts don't conflict with any that are already assigned -- I noticed some funny behaviour in NeoOffice, for instance. The window size is limited to the screen size. The window can be moved off the sides of the screen, but it can't disappear completely.

Quicksilver fans: You can move the window whilst the Quicksilver interface is open -- this is useful for dropping folders into open/save dialogs, etc.

That's it -- any constructive criticism on how the scripts are written and documented, or on how to make this idea better would be greatly appreciated. (Maybe an AppleScript application that opens on start-up and picks up the keystrokes? This would avoid Quicksilver being required for the shortcuts.)

[robg adds: I have mirrored the AppleScripts here on macosxhints.com, just in case the .Mac source ever goes away.]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080523040419784