OS X: How to Quickly Toggle Per-Monitor Spaces in Mavericks

TL;DR:

For those using Alfred App with the PowerPack enabled, I've put together a simple Workflow that changes and applies the setting when you type the magic word toggle-spaces into Alfred. There are no parameters, it just works. Download it now, or you can use the AppleScript at the bottom of this post.

A Quick History Lesson

Ever since the dawn of Lion, Mission Control and Full Screen mode, any Mac users with multiple monitors have been short-changed. Apple have gone some way to fixing that in OS X Mavericks, but with one massive downside. More on that shortly.

Mac OS 10.9 introduces a new option to the Mission Control prefpane. It's called "Displays have separate Spaces". It does pretty much what it says on the tin - each display is given its own set of spaces. So you can run a full screen app on your main screen, while freely switching spaces on the second screen. That's awesome! Right?

Wrong. For every positive there's a negative. If you have the setting enabled (as it is by default) you'll be able to utilise all displays fully but now you can no longer stretch windows across screens. If you have a window positioned between two displays, you will only see one half of it.

Butwhy?!

Why is this an issue? The most common use-case for me is when dealing with databases. Imagine you have a table with many, many columns. You want to see all of them without scrolling horizontally, but one screen isn't quite big enough. What do you do? You resize the window until everything fits. Well, you would if you could.

So we're now left with a tough decision: Better multi-display functionality, or windows spanning screens? Picking one wouldn't be too much of an issue, except changing the setting requires logging out and back in again to take effect.

That sucks, which is why I delved into the plists to find out what setting was being changed, and how I can "fake" the logout/login. Any time you need to span a window across two or more displays, run these two commands in the terminal:

defaults write com.apple.spaces spans-displays -bool TRUE
sudo killall -KILL loginwindow

To reverse the action, simply change TRUE to FALSE.

AppleScript to the Rescue!

Typing all that into the Terminal every time would be a nightmare. You could alias it, but you'd still have to switch to a Terminal window to run the alias. Alfred is probably the best solution, but for the cheapskates out there, paste this bunch of code into the AppleScript Editor. It's basically the same as the Alfred Workflow up in the TL;DR bit - you just need to run it manually instead.

  1. set target to "com.apple.spaces spans-displays"
  2.  
  3. if ((do shell script "defaults read " & target) is equal to "1") then
  4. set new_value to "FALSE"
  5. else
  6. set new_value to "TRUE"
  7. end if
  8.  
  9. do shell script "defaults write " & target & " -bool " & new_value
  10. do shell script "killall -KILL loginwindow &" with administrator privileges

That's All Folks!

It would be nice if Apple bothered to do something properly for a change, rather than leaving us to hack something together to fix their half-baked attempt at it. That probably won't happen though. I know some people may not necessarily need this functionality, but I'm sure there are many out there that do. If you're one of them, what do you use it for? I'd love to hear of some other use cases.