14.Jul.2009 Resize Windows in Mac OS X from the Keyboard Using AppleScript

I recently (about a year ago) switched back to using a Mac after using Windows throughout college. I haven’t looked back much, but I am disappointed by the lack of convenient window-resizing functionality on the Mac.

Specifically, what’s with having only one single window resizing tool on the bottom-right corner of each window? What if I want to expand my window upwards or leftwards? Then I have to move the window first, and then resize it. Also, the behavior of the green (+) button in the top-left corner seems to change randomly depending on which program you happen to be using. For example, in Firefox it works just like “maximize” in Windows, but in iTunes the same button toggles between a normal window and the “miniPlayer”.

All of this is especially annoying for those of us who switch back and forth between using the small, internal screen on our laptop, and hooking up to an external monitor at home or at the office. Every time you switch screens, lots of the open windows will either move around, or resize, or both. Some will be partially off of the screen. Sometimes (this often happens with iTunes after switching to a smaller monitor) the window will be too big, and one will not be able to reach the bottom-right corner for resizing. Very annoying.

My solution:

I did some research online, and found many helpful blog posts on the subject of resizing windows using AppleScript. I then wrote some AppleScripts which cause the currently active window to take on a specific size with respect to the current full size of your screen. I have linked to a zip file below which contains the three scripts I use most often: one to “maximize” the current window to fill the whole screen (maximize.scpt), and one each to make the current window fill the left or right half of the screen (leftHalf.scpt and rightHalf.scpt). These last two scripts are especially useful when one needs to work back and forth between two different windows.

Download the scripts here (.zip file)

Each time a script is called, the full screen size is re-detected, so this will work no matter how often you switch between laptop screen and external monitor.

The scripts can be bound to keyboard shortcuts of your choosing using some third-party application. I used Quicksilver, but I have read that it can also be done with Fastscripts Lite. Both of these programs are free. If you use quicksilver, you will want to use the “triggers” functionality. This page has some information and screenshots that show how to associate a script with a keyboard shortcut using Quicksilver triggers.

What about the dock?

All scripts respect a boundary on the top of the screen for the menu-bar. I like to keep my dock hidden, so the default behavior of the scripts is to put the bottom of the window at the very bottom of the screen. If you do not keep your dock hidden and you want the bottom your windows to be above the dock, I have included code in the scripts to detect the height of the dock. All you have to do is open each script in Script Editor (this can be found in Applications >> AppleScript >> Script Editor.app ), and change the word “false” to “true” in the line which reads

set dock_showing to false

Then save the changes. Note that this only works if you keep your dock on the bottom of the screen.

Which applications will this work for?

The scripts should be able to resize windows in the following applications with no extra steps: Safari, Firefox, Microsoft Word and Excel 2008, Script Editor, Terminal, Skype, iTunes, Skim, and probably many more.

I have not yet gotten the resizing to work for Microsoft Power Point 2008, or for Aquamacs (I may try to figure out an Emacs Lisp solution for Aquamacs in the future — check back for a post on that).

For some reason, AppleScript is, by default, not enabled for Preview. To enable AppleScript for Preview, enter the following line at the terminal prompt (note this should all be on one line):

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

This only needs to be done once. I found this information at this site.

One final note: my first attempts to resize finder windows resulted in either the top or the bottom of the windows being off the screen. So I added some code to the scripts which detects if the window being resized is a Finder window. If it is, some extra space is added so the window will not be cut off. I have not completely figured out when this extra space is necessary and when it isn’t. Please let me know in the comments if you have insight into this issue.

I hope you find this useful. Please share any questions/comments/feedback in the comments section below.

Other useful references not specifically linked to above:

1. Jeff Kelley’s Blog post “Resize Your Windows Automatically for Different Resolutions”. See also this update.

2. Mac OS X Hints post “Move and resize app windows via AppleScript”.

3. MacMembrane post “Resize Any Window Quickly and Exactly With AppleScript and FastScripts”.

4. Mac OS X Hints post “Create identical tiled windows in most apps via AppleScript”.

5. Jamie Matthews’ post on “How to get the dimensions of the dock”.

Thanks for your posts!