Build a Window Management App with Apptivate and AppleScript

Apptivate is an incredibly useful little app that lives in your menubar and allows you to set keyboard shortcuts to launch applications and scripts.

Today we’ll look at where to get Apptivate and how to set it up. As a bonus, I’ll show you how to create your very own window management application similar to SizeUp using a combination of AppleScript and Apptivate.

Installation

Apptivate is a completely free application available from Cocoabeans Software. To snag it, simply drop by the Cocoabeans site and click the download button.

screenshot

The Cocoabeans Website

Once you’ve downloaded Apptivate, drag it to your applications folder and double-click to launch the app.

How it Works

Once you’ve launched Apptivate, you should see an icon in your menu bar shaped like the command symbol (⌘). Click this icon to open the Apptivate menu.

screenshot

Adding Shortcuts

The Apptivate menu consists of a simple list with an area to record a shortcut and buttons for adding and eliminating items in the list.

To add an application, click on the little plus button. This should activate a window for you to select an application or script.

screenshot

Choosing an application

Navigate to your applications folder and select something to add. Next, select the application in the Apptivate menu and click in the field at the lower right to record a shortcut to launch the app.

screenshot

Click the field to record a shortcut

Now anytime you press that shortcut, the app will launch. That’s really all there is to it. No more, no less. Apptivate does one thing and it does it perfectly.

So What?

If you’re thinking “that’s it?” then you probably don’t appreciate keyboard shortcuts as much as you should. OS X’s default keyboard shortcut preference panel is quite limited in what you are allowed to create shortcuts to control. This app opens up the possibilities to almost anything on your hard drive.

My favorite part of Apptivate is that you can use it to run AppleScripts. This made it the perfect companion to a script that I had been working on. I’ll show you what I mean below.

DIY Window Management

I recently reviewed an app called Size Up that allows you to use keyboard shortcuts to resize windows in OS X. Being the nerd that I am, I couldn’t help but wonder if I could accomplish the same thing with AppleScript and save myself the $13. Sure enough, after a little tinkering I came up with a workable solution (largely inspired by this script).

However, aside from QuickSilver (which is overkill for many), I couldn’t find a decent and free way to launch the script via keyboard shortcut. Enter Apptivate.

The Basic Code

First of all, if you don’t know what AppleScript is, you should read our Ultimate Beginner’s Guide To AppleScript.

Once you’re familiar with the basic syntax, the script below should be pretty easy to interpret.

1
2
3
4
5
6
set activeApp to (path to frontmost application as Unicode text)
 
tell application activeApp
	activate
	set bounds of window 1 to {0, 21, 640, 800}
end tell

This is a super simple script that merely grabs the active app and sets its window size.

How it Works

The only tricky part in the code above is the “{0, 21, 640, 800}.” Each of these numbers represents the position of one of the edges of the window: left, top, right, and bottom respectively.

screenshot

The Numbers Explained

Note that these aren’t the size of the edges but their coordinates in pixels. So the numbers above will place a window’s left edge at the left edge of the screen, the top edge 21 pixels from the top of the screen, the right edge 640 pixels from the left edge of the screen and the bottom edge 800 pixels from the top of the screen.

The Menu Bar Blues

Now, the 21 pixels in the graphic above is significant. A window cannot occupy the same space as your menu bar. So a full size window begins at 0 pixels left but 21 pixels down (the height of the menu bar).

This makes our math all messy when we try to split the screen in half, especially since 21/2 is not a whole number. We’ll pretend this number is an even 22 to make things easier on ourselves. To compensate for the menu bar when splitting the screen in half, we’ll need to add eleven pixels to one window and take it away from the other. You’ll see how this works below.

Grabbing the Screen Size

Since different Macs have different screen sizes, we’ll have to code in an action to grab the screen size. It turns out this is fairly easy.

1
2
3
tell application "Finder"
	set screenSize to bounds of window of desktop
end tell

Now the variable screenSize will hold a number set identical to the one we saw above: {0, 0, 1280, 800}. As you can see, my screen size is 1280 x 800. Let’s put these numbers into some variables and declare a few extra variables that will help us later on.

1
2
3
4
5
6
7
tell application "Finder"
	set screenSize to bounds of window of desktop
	set theWidth to item 3 of screenSize
	set theHeight to item 4 of screenSize
	set halfWidth to (theWidth / 2)
	set halfHeight to (theHeight / 2)
end tell

Here I’ve set theWidth to the width of my screen, theHeight to the height of my screen and for good measure thrown in two more variables for cutting these in half.

The complete script putting everything together would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tell application "Finder"
	set screenSize to bounds of window of desktop
	set theWidth to item 3 of screenSize
	set theHeight to item 4 of screenSize
	set halfWidth to (theWidth / 2)
	set halfHeight to (theHeight / 2)
end tell
 
set activeApp to (path to frontmost application as Unicode text)
 
tell application activeApp
	activate
	set bounds of window 1 to {0, 0, theWidth, (halfHeight - 10)}
end tell

Eight Options

To keep it basic, I came up with eight window size and position options: four half screen options and four quarter screen options. The four half screen options are vertical left (1), vertical right (2), horizontal top (3) and horizontal bottom (4). So if we want a two up view of two vertical windows we would run our vertical left script followed by our vertical right script.

Similarly, the four quarter screen options are top left quadrant (1), top right quadrant (2), bottom left quadrant (3) and bottom right quadrant (4). Most of the script above will stay the same for every version, the only part that will change is the numbers. Here are the eight variations:

  • Half 1: {0, 0, halfWidth, theHeight}
  • Half 2: {halfWidth, 0, theWidth, theHeight}
  • Half 3: {0, 0, theWidth, (halfHeight – 11)}
  • Half 4: {0, (halfHeight + 11), theWidth, theHeight}
  • Quad 1: {0, 0, halfWidth, (halfHeight – 11)}
  • Quad 2: {halfWidth, 0, theWidth, (halfHeight – 11)}
  • Quad 3: {0, (halfHeight + 11), halfWidth, theHeight}
  • Quad 4: {halfWidth, (halfHeight + 11), theWidth, theHeight}

These measurements work for most applications, however, for whatever reason, Finder windows screw the whole thing up. This complicates things more than I wanted, but the fix isn’t so difficult. What we’ll have to do is tell AppleScript to run one set of numbers for Finder, and another set for all other apps. Here are the numbers for the Finder variation:

  • Half 1: {0, 0, halfWidth, theHeight}
  • Half 2: {halfWidth, 0, theWidth, theHeight}
  • Half 3: {0, 0, theWidth, (halfHeight – 33)}
  • Half 4: {0, (halfHeight + 33), theWidth, theHeight}
  • Quad 1: {0, 0, halfWidth, (halfHeight – 33)}
  • Quad 2: {halfWidth, 0, theWidth, (halfHeight – 33)}
  • Quad 3: {0, (halfHeight + 33), halfWidth, theHeight}
  • Quad 4: {halfWidth, (halfHeight + 33), theWidth, theHeight}

Putting It All Together

So now our entire script will look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
set activeApp to (path to frontmost application as Unicode text)
set pathToFinder to path to application "Finder" as text
 
--Get Screen Size
tell application "Finder"
	set screenSize to bounds of window of desktop
	set theWidth to item 3 of screenSize
	set theHeight to item 4 of screenSize
	set halfWidth to (theWidth / 2)
	set halfHeight to (theHeight / 2)
end tell
 
--Check Application and Set Parameters
if activeApp = pathToFinder then
	set windowParameters to {0, 0, theWidth, (halfHeight - 33)}
else
	set windowParameters to {0, 0, theWidth, (halfHeight - 11)}
end if
 
--Resize Window
tell application activeApp
	activate
	set bounds of window 1 to windowParameters
end tell

Notice that I’ve setup an “if” statement that checks the active application and sets the parameters accordingly. This snippet compares the path of the current application to the path of your Finder and the chooses how to proceed.

Now that we’ve finished our basic structure, create a folder on your Mac and save the eight different versions of the script (with the appropriate parameters from above) as separate files. Alternatively, you could combine them into one script and include an option for choosing between them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
set activeApp to (path to frontmost application as Unicode text)
set pathToFinder to path to application "Finder" as text
 
 
--Get Screen Size
tell application "Finder"
	set screenSize to bounds of window of desktop
	set theWidth to item 3 of screenSize
	set theHeight to item 4 of screenSize
	set halfWidth to (theWidth / 2)
	set halfHeight to (theHeight / 2)
end tell
 
--Get Desired Window Size
set theChoices to {"Half 1", "Half 2", "Half 3", "Half 4", "Quad 1", "Quad 2", "Quad 3", "Quad 4"}
set selectedSize to {choose from list theChoices} as text
 
if activeApp = pathToFinder then
 
	if (selectedSize = item 1 of theChoices) then
		set windowParameters to {0, 0, halfWidth, theHeight}
	else if (selectedSize = item 2 of theChoices) then
		set windowParameters to {halfWidth, 0, theWidth, theHeight}
	else if (selectedSize = item 3 of theChoices) then
		set windowParameters to {0, 0, theWidth, (halfHeight - 33)}
	else if (selectedSize = item 4 of theChoices) then
		set windowParameters to {0, (halfHeight + 33), theWidth, theHeight}
	else if (selectedSize = item 5 of theChoices) then
		set windowParameters to {0, 0, halfWidth, (halfHeight - 33)}
	else if (selectedSize = item 6 of theChoices) then
		set windowParameters to {halfWidth, 0, theWidth, (halfHeight - 33)}
	else if (selectedSize = item 7 of theChoices) then
		set windowParameters to {0, (halfHeight + 33), halfWidth, theHeight}
	else if (selectedSize = item 8 of theChoices) then
		set windowParameters to {halfWidth, (halfHeight + 33), theWidth, theHeight}
	end if
 
else
 
	if (selectedSize = item 1 of theChoices) then
		set windowParameters to {0, 0, halfWidth, theHeight}
	else if (selectedSize = item 2 of theChoices) then
		set windowParameters to {halfWidth, 0, theWidth, theHeight}
	else if (selectedSize = item 3 of theChoices) then
		set windowParameters to {0, 0, theWidth, (halfHeight - 11)}
	else if (selectedSize = item 4 of theChoices) then
		set windowParameters to {0, (halfHeight + 11), theWidth, theHeight}
	else if (selectedSize = item 5 of theChoices) then
		set windowParameters to {0, 0, halfWidth, (halfHeight - 11)}
	else if (selectedSize = item 6 of theChoices) then
		set windowParameters to {halfWidth, 0, theWidth, (halfHeight - 11)}
	else if (selectedSize = item 7 of theChoices) then
		set windowParameters to {0, (halfHeight + 11), halfWidth, theHeight}
	else if (selectedSize = item 8 of theChoices) then
		set windowParameters to {halfWidth, (halfHeight + 11), theWidth, theHeight}
	end if
end if
 
--Resize Window
tell application activeApp
	activate
	set bounds of window 1 to windowParameters
end tell

This script will give you a nice little list to select a window size option.

screenshot

Choosing Your WIndow Size

Setting it Up in Apptivate

Now follow the same exact steps we took above to add your scripts to Apptivate. If you created eight different scripts, drop them all in and assign shortcuts. I opted for the shorter route and used the combined script above that way I only have one item to add and one keyboard shortcut to remember.

screenshot

Adding Your Script to Apptivate

Voila! You should now have your very own window management script. If everything works, you should easily be able to create custom window arrangements on the fly in several applications.

screenshot

Adding Your Script to Apptivate

How Will You Use Apptivate?

This is just one of a nearly unlimited number of uses for Apptivate. Leave a comment below and let us know how you plan to use it in your own workflow.

Also, if you’re familiar with AppleScript, be sure to leave a comment on how you would improve the window management script. It’s pretty primitive in its current state and may need some tweaking for different screen sizes and/or resolutions so I’d love to hear your suggestions!