Size windows correctly with an easy Applescript


Size windows correctly with an easy Applescript
Quick Mac Tip, 09 October 2011

When I make screenshots for MacTips I prefer to end up with images that are 450 pixels wide or less. If they’re wider I have to open my graphics editor, Acorn, and resize them manually.

That’s why I try to make windows the right size to start with.

One way to do that is by trial and error, but a much better way is with a very little bit of AppleScript. Here’s how.

Set the bounds of a window

To start with just experiment with the Finder, though this AppleScript should work with most apps on your Mac. It may not work with all of them though.

Open up AppleScript Editor in your Utilities folder. A new document window appears. Also open a new Finder window. It can be any size and placed anywhere on your screen.

Type in the following AppleScript:

set bounds of front window to {0, 22, 450, 248}.

set bounds of front window to {0, 22, 450, 248}.

Then run the script and see what happens. You should see your Finder window move to the top left of the screen, and it should be 450 pixels wide and 226 pixels high.

tell application "Finder"
	set bounds of front window to {0, 22, 450, 248}
end tell

My screenshots include a line that says activate. You can leave that line out — it just brings that application to the front and makes it active. If you leave it out the application will stay in the background.

How the bounds measurements work

The bounds reflect the co-ordinates in pixels for the top left and bottom right corners of the window. In my example above I set the top left corner of my window to start 0 pixels across from the left edge of the screen. The Menu Bar takes 22 pixels, so I set the top left corner to start 22 pixels down from the top edge of the screen.

This screenshot is 450 * 280 - the window itself is a little smaller.

This screenshot is 450 * 280 – the Finder window itself is a little smaller. My script set the window bounds to {0, 22, 425, 235}.

Experiment with numbers less than 22, but in my testing smaller numbers had no effect.

The second pair of numbers reflect the bottom right corner: 450 pixels from the left and 248 pixels down from the top (meaning 248 pixels starting below the Menu Bar).

Get the correct co-ordinates

You might need to do things the other way round: perhaps you’ve already made a window a certain size that works for you and now you want to know what bounds to set. It’s simple. Make a new Script that says:

tell application "Finder"
	get bounds of front window
end tell

Then run the script and look at the results in the lower part of the AppleScript Editor window. You’ll see something like this: {962, 44, 1616, 471}.

You may have to visit the View menu in the Menu Bar and select Show Result.

Script Editor Result section shows the window size.

Script Editor Result section in the lower part of the window shows the Finder window bounds.

Try the window bounds with other apps

In the scripts above replace Finder with the name of the app whose window bounds you want to get or set. Many apps will ‘behave’ and do what you expect, but some don’t understand AppleScript and you’ll be out of luck.

Remember to save your scripts

Remember to save your scripts if you think they might be useful in future. When you choose Save just choose Script for the File Format.

Tell us in the comments how this Tip helped you.

Related posts

[wpzon keywords="pressure cooker" sindex="PCHardware" snode="1232597011" sort="salesrank" listing="8"]


Top