Manage windows on the Mac
Keep windows neat with OS X’s own built-in scripting language AppleScript
AppleScript will keep your Finder windows in place and under control
SKILL LEVEL Can be tricky
IT WILL TAKE 20 minutes
YOU’ll NEED AppleScript Editor
Window management in OS X hasn’t improved in a long time. Lion introduced the ability to resize windows from any edge or corner, but this is still fiddly at times – especially when working with a trackpad – and it doesn’t hold a candle to something like the convenient shortcuts baked into recent versions of Microsoft Windows, which allow you to quickly snap a window to fill part of the desktop.
There’s a good selection of third-party utilities for OS X that help you manage windows, including BetterTouchTool/BetterSnapTool (boastr.net), Moom (manytricks.com/moom) and Divvy (mizage.com/divvy). However, if your needs are simpler – say, you regularly want to position two windows in the left and right halves of your desktop, respectively – all you really need is AppleScript, which is built into OS X.
OS X comes with two tools for automating tasks. You might already have used Automator, which is the easiest to learn. The other is AppleScript Editor (located in /Applications/Utilities), which can talk to apps in greater detail using AppleScript, a scripting language that can control elements of OS X. Some apps provide their own dictionary that defines words and phrases for things that you can query and control with AppleScript, but many more apps can be controlled using a vocabulary for common objects, such as windows, that OS X makes available to all.
Open AppleScript Editor and choose File > Open Dictionary. First choose the App Store app from the list of those detected on your system. The window that opens has three panes across the top. The leftmost one lists only Standard Suite, Text Suite and Type Definitions. Choose Open Dictionary again and choose Safari. It understands an additional set of phrases related to its features, which are described to you when you choose ‘Safari suite’.
OS X’s window resizing is fiddly with a trackpad, and it doesn’t hold a candle to Windows’ shortcuts
AppleScript is designed to read a lot like a natural language, the intention being that it’s easier to follow than scripting languages laden with punctuation and other esoteric syntax. Getting started can be daunting, even though Apple provides a bunch of example scripts with OS X and a comprehensive programming guide (see Jargon Buster). Studying simple scripts that already exist is a good way to get a grasp of AppleScript, and we’re going to show how you can use it to deal with the universal problem of managing windows on the desktop.
We’re going to write our scripts within Automator, using its Run AppleScript action. Triggering scripts from the keyboard makes them an efficient enhancement to OS X. AppleScript Editor’s preferences include an option to add a Script menu to the menu bar to run your scripts, but we’ve experienced trouble getting keyboard shortcuts assigned to items in that menu to work. We’ll show you how to run AppleScripts from OS X’s Services menu to overcome this issue. The simplest of our scripts let you resize a window to a specific region of the screen by pressing your choice of modifier keys – ß,≈,å and ç – and a character key.
One thing our scripts won’t address well is multi-display setups, which complicate the coordinate system depending on how the displays are laid out in > System Preferences > Displays > Arrangement. Professionally developed tools can deal with this, and are your best option. However, if your Mac’s primary display is arranged leftand topmost in the Displays pane, or by temporarily disconnecting additional displays, you’ll still be able to benefit from this tutorial.
AppleScript provides a vocabulary through which you can take control of windows and other abstractions.
The Scripts menu enables you to quickly run scripts from the menu bar, or by using a keyboard shortcut.