Linkedin Profile
Tags:
programming
seattle
things that bug me
wall art

9/2008
8/2008
7/2008
6/2008
5/2008
4/2008
3/2008
2/2008
1/2008
12/2007
11/2007
10/2007
9/2007
8/2007
7/2007
6/2007
5/2007
4/2007
3/2007
2/2007
1/2007
12/2006
11/2006
10/2006
9/2006
8/2006
7/2006
6/2006
5/2006
4/2006
3/2006
2/2006
1/2006
12/2005
11/2005
10/2005
9/2005
8/2005
7/2005
6/2005
5/2005
4/2005
3/2005
2/2005
1/2005
12/2004
11/2004
10/2004
9/2004
8/2004
7/2004
6/2004
5/2004
4/2004
3/2004
2/2004
1/2004
12/2003
11/2003

Sync
2008-02-10

Last night I wrote a script to syncronise my Google Calendar and my Motorola Razr cell phone. I didn't really set out to do this, but it seemed like a useful thing to do and like all fun problems it got its teeth into me and I couldn't resist. It all started when my wife tried to send me a photo from her phone via bluetooth. In the course of trying to get the photo I paired up my own cell phone and my new laptop. Yes, new laptop. I bought in a new MacBook last week. It is a beaut. I am still setting it up and getting comfortable in the mac environment, so this was a good way to learn some things.

Google Calendar to iCal

So now I have my phone paired with my laptop, and iSync is syncing my calendar from iCal to the phone. Neat. Only problem is that I don't keep my schedule in iCal, I keep it in Google Calendars.

Getting a schedule from Google Calendar to iCal is pretty easy. See this article. The only caveat is that if you are a Google Apps For Your Domain customer, like me, then you might not see a "Share all information on this calendar" option for your calendar. By default this is switched off for the entire domain, so you will have to visit partnerpage.google.com/your_domain and click in the link on the top right that says "Manage this domain", from there go into the Calendar section and choose one of the "Share all information..." options. And then go get a cup of coffee. It took 20 suspenseful minutes before this change was reflected in my calendar and I was able to sync it, so have patience!

Lazy

OK, at this point I can open up iCal and ask it to refresh my calendar from google (control-shift-r), and I can then open up iSync and transfer that calendar over to my phone (command-t). Only problem is that it involves a few mouse clicks or key strokes to open the apps, and then a few key combinations to transfer data. I'm too lazy for that. What I really want is to press a single button and have the whole thing proceed automatically. I'm an absolute beginner on the mac, so I hunted around and decided to try it in AppleScript.

Automatically Sync iCal

First up, here is a script to sync iCal:

tell application "iCal"
	activate
	tell application "System Events"
		keystroke "R" using {command down, shift down}
	end tell
	delay 5
	quit
end tell

(I got most of this from here.) This is not a pretty script. Particularly, the "delay 5" loop is a crummy way of letting iCal sync before quitting. And there isn't an API for refreshing a subscribed calendar, so we are left with sending key strokes, which fails at times. But this works well enough.

Automatically Sync Phone

Here is a script to perform the next step, pushing the schedule onto the phone:

tell application "iSync"
	activate
	synchronize
	repeat
		if not syncing then exit repeat
	end repeat
	quit
end tell

This script is much neater. I took both of these scripts and converted them into a single application using the Script Editor (Applications -> AppleScript -> Script Editor) and saved the new application in my applications folder. The option is under File -> Save As. I can now run one program to sync my schedule onto my phone!

Mapping a Key

All that remains is to map a keyboard shortcut to the application I just built. To do this I used QuickSilver, which makes it easy to set up "triggers" (keyboard shortcuts) to do just this sort of thing. See the tutorial here.

Of course I could just get an iPhone, in which case this would undoubtedly work with less rickety scripting. Until then...

Back to weblog