Saturday, August 6, 2011

Side-by-side windows on a Mac

I have a friend who uses a Windows machine at work, and an Apple laptop at home. She told me about a new feature in Windows 7 that allows you to snap windows to the left half or the right half of the screen, giving you the ability to view two windows side-by-side. Being a proud Apple user, I told her that I could give her a similar feature on a Mac. Below are my instructions for Snow Leopard 10.6.7 (I will update these instructions for Lion if need be), using a couple of AppleScripts and the Automator. I found this link very helpful when figuring out how to create a keyboard shortcut of an AppleScript.

Desired Outcome:

By holding down the key combination Shift-Command-Comma or Shift-Command-Period, you will be able to snap the active window to the left half or to the right half of the screen, respectively. This will allow you to show two windows side-by-side, which is nice for comparing two documents (or for having the baseball game scores and your work document open side-by-side).

Step 1 - Open Automator

Automator is located in Macintosh HD > Applications. It has a cute little icon of a robot holding a pipe (representing workflows, I guess. Or maybe it is a bazooka). Double-click that little guy.

Step 2 - Select "Service"

The first thing you see should be a dialog for choosing a template. Choose "Service" (the gear icon) and click Choose.

Step 3 - Set the Input

We want to be able to launch the AppleScripts no matter where we are or which application we are currently runnning. Therefore, you need to set "Service receives selected" to "no input" in the drop down combo-box. You may leave the other comb-box as "any application".

Step 4 - Insert the AppleScript

In the search box with the magnifying glass, type "Run AppleScript". This should give you one hit. Drag that action into the large gray workspace which says "Drag actions or files here to build your workflow".

Delete the line that says (* Your script goes here*), and copy and paste the following code into that section:


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"
set frontmostApplication to name of the first process whose frontmost is true
end tell

tell application frontmostApplication
activate
set the bounds of the first window to {0, 0, screen_width / 2, screen_height}
end tell


You can test it out by hitting the green play button. It should snap your current window (which in this case is the automator) to the left half of your screen. If you want to leave some borders, you can modify the bounds of the windows in the script.

Step 5 - Save as Service.

Choose File > Save as ...
Give it a name, such as Tile_Left

Step 6 - Create a Tile_Right script

Go to File > New.
Repeat steps 2 through 5. This time, you will insert the following code:


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"
set frontmostApplication to name of the first process whose frontmost is true
end tell

tell application frontmostApplication
activate
set the bounds of the first window to {screen_width / 2, 0, screen_width, screen_height}
end tell


Save the file as a service. I called it Tile_Right

After this, you may quit Automator. Almost done!

Step 7 - Assign keyboard shortcuts to Tile_Left and Tile_Right

Open System Preferences (Go to Apple icon in top left corner and select "System Preferences..." Choose Keyboard > Keyboard Shortcuts > Services

In the right window, scroll down until you see your new services. Mine show up at the bottom under General. I called mine "Tile_Left" and "Tile_Right". Make sure the check marks are on for both of them.

To assign a keyboard short cut, highlight the action by clicking it once. Then click once on the right side of the highlighted region where the key commands will go (I know this is a confusing explanation - see picture for some guidance). Enter the key combination you would like to use. Note that some key combinations cannot be used or are already taken. I mapped Tile_Left to Shift-Command-Comma and Tile_Right to Shift-Command-Period. Just hold down those three keys and the symbols should show up to the right of the action.

Setting up keyboard shortcuts for AppleScript actions


Step 8 - Try it out

Try out the new keyboard shortcuts. The active window should snap to either the left or the right half of the screen. Post some replies if you are having trouble, and I will try to answer them. Hope you like it!

1 comment: