Everyone is all abuzz with news of the SmacBook Pro hack. Very very cool.
I wanted to have a play with it, but Amit’s AMSTracker doesn’t appear to work on my 15″ MBP.
Fortunately, someone else hacked together a little SMS reader that does work on Intel. Unfortunately, it is a different format than what AMS emits and it only reads once and spews. Fortunately, I know my way around C and fixed it (tarball of source).
The SmacBook Pro hack uses a perl script to interpret the output of AMSTracker. Since the output is different for motion and the script is so trivial, I rewrote it in Python and used PyObjC to send the notification directly from the script. Requires Python 2.4 (or copy the subprocess module from 2.4) and PyObjC:
#! /usr/bin/python from Foundation import * import subprocess import time # assumes the motion executable is named 'motion-bin' and is CWD pipe = subprocess.Popen(["./motion-bin"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout = pipe.stdout stable = 0 while 1: line = stdout.readline() x = int(line.split()) if abs(x) < 20: stable = stable + 1 if (abs(x) > 30) and (stable > 30): stable = 0 if x < 0: notificationName = "SwitchToPrevWorkspace" else: notificationName = "SwitchToNextWorkspace" print notificationName NSDistributedNotificationCenter.defaultCenter().postNotificationName_object_(notificationName, None)
Running the above script will print "SwitchToPrevWorkspace/SwitchToNextWorkspace" whenever you smack your machine. If the modified version of Desktop Manager is running -- it was trivial to make it build universal with the additional notification observer -- then it'll switch desktops with every smack. You'll want to configure Desktop Manager to have one row of desktops and for the animation to be a slide between desktops.
It actually works quite well, though it'll occasionally slide the wrong direction. Blame the math (or lack thereof).
And, of course, the above demonstrates how trivial it would be to make various physical gestures control other aspects of your machine. Since I don't actually use multiple desktops, I replaced the notification stuff with a call out to applescript to play the next track in iTunes.
I can't take any credit for any of this. I just glued together a slightly different set of parts to make it all work on my MacBook Pro. While it works really well, it is far from totally useful. It busy-reads from the motion sensor, spewing new values every few milliseconds. As a result, leaving this thing running eats battery life while chewing up a bunch of CPU.
Fun toy, though.