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, Mail.app, 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.
Wednesday, May 21, 2008 at 6:13 pm
[...] 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 [...]
Thursday, May 22, 2008 at 2:10 am
On Panther, I get this error:
Can’t get bounds of window of desktop.
Any ideas?
Thanks, Martin
Thursday, May 22, 2008 at 8:19 am
Martin, I did some googling but haven’t found anything yet. You might try asking in the Lifehacker post about this.
Thursday, May 22, 2008 at 10:41 am
This is neat! Thanks.
Thursday, May 22, 2008 at 11:23 am
Thanks Jeff, I’ll do that.
Thursday, May 22, 2008 at 12:06 pm
Thanks for this valuable contribution to be happier with my computer.
Thursday, May 22, 2008 at 4:31 pm
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?
Les
lesliebradley@comcast.net
Thursday, May 22, 2008 at 4:35 pm
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.”
Thursday, May 22, 2008 at 5:27 pm
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?
Thursday, May 22, 2008 at 6:20 pm
@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.
Friday, May 23, 2008 at 1:00 am
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.
Friday, May 23, 2008 at 2:01 am
[...] 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 [...]
Friday, May 23, 2008 at 7:54 am
Martijn,
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.
Saturday, May 24, 2008 at 5:20 pm
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.
Sunday, May 25, 2008 at 8:18 am
[...] 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 [...]
Sunday, May 25, 2008 at 8:58 pm
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?
Sunday, May 25, 2008 at 10:02 pm
PowerLlama,
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.
Sunday, May 25, 2008 at 10:43 pm
Thanks Jeff!
I know pretty much nothing about applescript, or coding. =(
I think I can work it out though.
Monday, May 26, 2008 at 12:17 pm
[...] code snipped might be useful for anyone wanting to write a script to resize your application windows automatically to fit on your [...]