Resize Your Windows Automatically for Different Resolutions

Tuesday, May 20, 2008

I use my MacBook Pro in a few different scenarios: by itself, plugged in to a 21” Apple Cinema Display, or plugged in to a 24” Dell 2405FPW.  I’m also rather OCD; I prefer my Firefox/Safari,, and Vienna windows to be centered, stretch from the menu bar to the top of my Dock, and be a certain width.  I created a small AppleScript to auto-detect my resolution and size the windows accordingly:

tell application "Finder" set screen_resolution to bounds of window of desktop set screen_width to item 3 of screen_resolution set screen_height to item 4 of screen_resolution end tell tell application "System Events" to tell process "Dock" set dock_dimensions to size in list 1 set dock_height to item 2 of dock_dimensions end tell set desired_width to 1400 set side_space to screen_width - desired_width set left_bound to (side_space / 2) set right_bound to left_bound + desired_width set bottom_bound to screen_height - dock_height set top_bound to 22 (* for the menu bar *) try tell application "iTunes" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try try tell application "Firefox" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try try tell application "Mail" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try try tell application "Vienna" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try

With that in place, I saved it as an application in ~/Applications, and put it in my Dock. Now, whenever I change resolutions, I just click the button and everything is how I like it.

To change the script, you should be able to add any application with an AppleScript dictionary that supports moving and sizing the window.  The numbers I’ve used make the windows 1,400px wide, and the height that you want will depend on the size of your Dock. The script moves windows to the center, desired_width wide, and from the menubar to the Dock.

Note: I have had some trouble recently; sometimes when I change my resolution the AppleScript doesn’t pick it up.  To combat this, I told the Displays System Preferences pane to keep its icon in the menu bar; when my script uses the incorrect resolution, I change my screen resolution then change it back, which is enough for the script to detect the change.

Update 2008-05-28: Made some usability changes. Details here.


24 Responses to “Resize Your Windows Automatically for Different Resolutions”

  1. […] been looking for similar functionality for your Mac, Jeff’s script is a good starting point. Resize Your Windows Automatically for Different Resolutions [Jeff Kelley’s […]

  2. On Panther, I get this error:

    Can’t get bounds of window of desktop.

    Any ideas?

    Thanks, Martin

  3. Jeff Kelley Says:

    Martin, I did some googling but haven’t found anything yet. You might try asking in the Lifehacker post about this.

  4. Thanks Jeff, I’ll do that.

  5. unnikuttan Says:

    Thanks for this valuable contribution to be happier with my computer.

  6. Les Bradley Says:

    Didn’t the window re-size button in early Mac OS toggle between two sizes — at least the default and the last size you set? When I resize in Leopard, a window fills the entire screen, which is a little ridiculous on a 24-inch iMac.

    I look forward to trying your script. I envision having a few different settings for various screen sizes and locations. I don’t know I have the expertise to write such a script yet, though I’m willing to try. In the meantime, anyone else want to give it a shot?


  7. d Says:

    Any idea why my applescript script editor is complaining about the following line on 10.5.2?

    tell application “Finder”

    error : “Expected expression, property or key form, etc. but found unknown token.”

  8. qs7 Says:

    Very cool Jeff!

    However, I’m having trouble in Leopard running the script. It reports that “The Classic environment is no longer supported”. I’m kind of a noob on AppleScript. I just copied your script to a text file and saved is as a (.app) application.

    Any thoughts?

  9. Jeff Kelley Says:

    @d: Try deleting the quotation marks, then putting them back in and pressing “Compile.” Apparently they aren’t copying correctly.

    @qs7: You need to open Script Editor, which is typically in /Applications/AppleScript. From Script Editor you can save it as an .app. Good luck! Check out this page for some more details.

  10. Martijn Says:

    Great script, thanks!
    What would happen if you have 2 different resolutions at the same time? Normally when I connect my Cinema display to the MBP, I have 1440*900 on the MPB and 1920*1200 on the Cinema.

  11. […] pasarnos un tiempo precioso reordenando iconos de forma que entren en la nueva resolución tenemos este Applescript creado por Jeff Kelley’s. que detectara la resolución que tengamos cambiando el tamaño de los […]

  12. Jeff Kelley Says:


    When you have multiple displays, it acts as one large display. If you have your displays next to one another, it’ll be something like 3360 x 1200. Look here for more information.

  13. notreadbyhumans Says:

    Cool script.

    Anybody have any idea whether it’s possible to also adjust the desktop Grid spacing or Icon size through Applescript?

    It would be very useful to also be able to automatically adjust the layout of my desktop as I switch between monitors.

  14. […] cambio de resolución puede ser un poco fastidioso. Una posible solución es este AppleScripts de Jeff Kelly, que detecta automáticamente la resolución del monitor y cambia las ventanas con el tamaño […]

  15. PowerLlama Says:

    My question is, can I have it resize the different programs differently? I like mail to take up an entire screen, but I like firefox smaller. Would that require me to make two separate scripts, and maybe an applescript to run both of them? Or can this be done in one script?

  16. Jeff Kelley Says:


    There’s no reason you can’t; my script uses variables like _nl and _nr, you could use _new_firefox_left and _new_mail_left, etc. Go nuts.

  17. PowerLlama Says:

    Thanks Jeff!

    I know pretty much nothing about applescript, or coding. =(

    I think I can work it out though.

  18. […] code snipped might be useful for anyone wanting to write a script to resize your application windows automatically to fit on your […]

  19. Ben Says:

    Thanks Man! This code helped me in some automation testing I am doing. you rock!

  20. […] po’ lungo questo AppleScript, più da capire che ripetere però. Lo ha scritto Jeff Kelley, professionista che spesso deve cambiare risoluzione di schermo e si è stufato di aggiustare […]

  21. NEX Says:

    I have a problem with the connection of an external monitor in my
    macbookpro. at MiniDisplay port is connected to the TV HDMI port
    LCD 22 “LG. The TV, according to the user manual, works with a
    resolution of 1280 x 800 but the macbook does not give me that option
    among the available resolution. has a 1200 something and another
    1400 … but not that of 1280. my question is: with this script I can
    change the resolution for my LCD?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: