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!


  • adrian

    this window management idea would work well with the speech recognition engine built into mac os X. The engine decodes speech and looks for a file in the speech folder whose name was spoken and then opens it, so it works well with applescript, web locations and automator actions.

    • adrian

      sorry for the double comment, but i just tried the code, and it bothered me a bit that i had to click a what size I wanted. how would you make a script for the individual options

    • adrian

      Actually I figured it out and I really like the result. The way I set it up, each of the numpad keys is assigned a window position, depending on its position on the numpad. For example, ⌘⌥⌃7 is top left quad because it is in the top lef corner of the numpad. all the other keys follow that pattern, and then numpad 5 is fullscreen, {0, 0, theWidth, theHeight}.

      PS, I don’t have any problems with Finder on a 1680 by 1050 screen.

    • Adrian

      Really sorry about the multiple posts, but I think this will be helpful, I have uploaded my scripts to git and added finder support as mentioned in the tutorial http://github.com/adrusi/Window-Management-with-Apptivate-or-Speech-Recognition-Engine

      • http://www.designshack.co.uk Joshua Johnson

        Thanks Adrian! Great Work!

  • http://novastormsoftware.com Justin Mrkva

    This would be perfect, until you try to use it with different-sized or multiple monitors. I love SizeUp, and it’s relatively inexpensive and well worth the money for what it does. That said, if you want to go the DIY route, this is a good tutorial. There’s a lot more you can do with AppleScript once you really dig deep.

  • http://www.theronburger.com/ Theron Burger

    This seems like an awsome idea.
    Divvy changed my life. It upped my productivity simple by dividing the screen in two, and made all those fancy two panel, tabbed finder replacements obsolete
    That said I seem to have a little problem with divvy slowing my computer down. Not sure if i am imagining it, but when i am on my 23″ screen, the applications stack staggers as it pops out of the dock if i am running divvy. Anyone else see this problem?
    Divvy also seems to have some clash with magicprefs.

  • Elad

    I use Spark for so long for the same tasks…
    Can’t find any differences between the two… :)

    • http://lanegoldberg.com Lane

      Spark is more powerful.

  • Pingback: Build a Window Management App with Apptivate and AppleScript | MacMerc

  • Paragumba

    it took years to find this site, but there it is:
    exactly what i had in mind and someone explaining how to do it.
    i think i’m going to figure out how to set only the upper left corner now.

  • Staffchild101

    I used to use Divvy but now there is absolutly no point. I have all my shortcuts keyed to cmd + 1,2,3,4,5 and it is a lot better. The less clicks the better.

    • Staffchild101

      Note, these scripts work fine with finder, in fact I dont think I needed to alter them but I did anyway, but I tried to use it with an open Automator window and it screwed it up. I had to shut it down to get control of the top bar again. Any ideas?

  • Pingback: Best of AppStorm in June | iPhone.AppStorm

  • Pingback: Best of AppStorm in June | Mac.AppStorm

  • Pingback: Best of AppStorm in June | Design City

  • Pingback: Best of AppStorm in June | Web.AppStorm

  • Pingback: Best of AppStorm in June - Programming Blog

  • Pingback: Best of AppStorm in June | Mac Popular

  • Ouroboros

    It is possible to make windows snapback to their original size and positions with this method? If is how?

    • Ezea

      I don’t know if this will still be useful to you, but if you add another item to the list theChoices named “Full” or something. Give it parameters of {0, 21, theWidth, 800}. That should snap it back to full screen.

  • http://www.martinvaresio.com.ar Martin Varesio

    Anyone else see this problem?

  • Mike O’Fyle

    Apptivate is not free :(

theatre-aglow
theatre-aglow
theatre-aglow
theatre-aglow