Syncing the iPhone with Google Calendar (now with working Alarms!) 

My biggest gripe with the iPhone thus far has really been a gripe with iCal. Namely, that there's not an easy way to add alarms to subscribed calendars. I know that sounds like a silly gripe, but let me set the stage.

My main calendaring info is in Google Calendar, like a lot of folks. This is great because I can access my calendar from anywhere, on any computer, most cell phones, etc. It gives me a central way to maintain a calendar and have access to it pretty much all the time. I subscribe to my Google Calendar in iCal on my Mac, which gives me native calendaring (one-way, at least) that's always up-to-date with the data in the Google cloud. It's nice.

Taking it one step further, I use SyncMyCal to push my Outlook calendar to my Google Calendar as well. (I would use the normal Google Calendar Sync application, but it only syncs with the primary calendar, and I'd rather keep my personal calendar and work calendar separate.) So, every day, before I leave the office, I click a little sync button and it pushes my Outlook info up to Google Calendar. Again, one-way sync, but it's one-way from my primary source, so I'm not worried about it.

That means I've got my personal and work calendars all centrally located on Google Calendar, accessible from pretty much anywhere. Including my primary machine of my Mac, where iCal subscribes to all of my various calendars. It's a wonderful system.

Except one thing. iCal doesn't allow you to set alarms on subscribed calendars. At least not through the interface. That makes this wonderful sync system decidedly less useful. See, between my MacBook and my iPhone, I'm pretty much covered. One is with me most of the time. If I had alarms on my calendars, then I'd pretty much have a perfect setup.

It worked incredibly well with my Motorola Q (and The Missing Sync from Mark/Space), which would import all of the calendar items from iCal, and set default alarms on them on the Q. Beautiful.

Not so much on the iPhone. For a couple of months, I've just dealt with the fact that using Google Calendar put me in the middle of the two supported options: using iCal and using Exchange (which became available with the 2.0 firmware). I could see the calendar events on my iPhone, but they were never going to make that nice "bleep bleep" sound and let me know that I had a meeting or I had to be somewhere in a few minutes. It's something I had taken for granted with previous smartphones, but just chalked up to a deficiency in the early years of the iPhone.

Except I finally got fed up enough to do some digging into AppleScript and found some pointers to adding an alarm to an event. I figured why not give it a shot on subscribed calendars? Maybe I could add some alarms to my subscribed calendars?

After poking around and playing with AppleScript (something brand new to me, I got this working):

tell application "iCal"
	set theCalendars to {"Subscribed 1", "Subscribed 2"}
	repeat with theCurrentValue in theCalendars
		tell calendar theCurrentValue
			set theEvents to every event
			repeat with theCurrentEvent in theEvents
				tell theCurrentEvent
					make new sound alarm at end with properties {trigger interval:-15}
				end tell
			end repeat
		end tell
	end repeat
end tell

Basically, we grab our two subscribed calendars (those are placeholder names, replace with your own subscribed calendar names), go through each entry and add a sound alarm 15 minutes before the event. It takes maybe 10 or 15 seconds to go through both of my calendars, but lo! I end up with alarms both in iCal and on the iPhone!

Bingo. Exactly what I need. I'm sure there's something more elegant, but this worked for me.

The next issue, of course, was a "race condition" of sorts. My calendars update themselves periodically. If they updated before I sync with my iPhone, the alarms would be gone. How could I resolve that?

How about another AppleScript? I did a bit more googling, and of course, there's a nice way to sync your iPhone (or iPod), via AppleScript. Why not combine both scripts and drop it into the iTunes script directory? That's a brilliant idea!

tell application "iCal"
	set theCalendars to {"Sub1", "Sub2"}
	repeat with theCurrentValue in theCalendars
		tell calendar theCurrentValue
			set theEvents to every event
			repeat with theCurrentEvent in theEvents
				tell theCurrentEvent
					make new sound alarm at end with properties {trigger interval:-15}
				end tell
			end repeat
		end tell
	end repeat
end tell

tell application "iTunes"
	repeat with s in sources
		if (kind of s is iPod) then update s
	end repeat
end tell

Now, when I'm about to leave the house, I just do this:

iTunes Sync

That's it. All of my calendar items, from Google Calendar, sync'd to my iPhone with alarms. It's a beautiful thing.

And I wish nothing more than for iCal to render it useless my having a "add default alarm to subscribed calendar" checkbox.