|
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 iCalSo 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! LazyOK, 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 iCalFirst 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 PhoneHere 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 KeyAll 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... |
|