March 26, 2008 at 10:30 PM by Dr. Drang
My iPhone was delivered on Monday, and I’ve been making changes in my workflow to accommodate it. One of the changes was to write an AppleScript that recategorizes events in iCal. Now I can add calendar items on the iPhone and get them assigned to the proper calendar in iCal after syncing.
Let me start by describing my iCal setup. I have two main calendars, called “work” and “home,” that are fairly dynamic; a few nearly static calendars that hold holidays, birthdays, and anniversaries; and one other calendar, called “from-phone,” that I created long ago with the intention of collecting events sync’d from my cell phone. Until this week, though, that calendar had never been used because creating entries on my previous phones was such a pain in the ass that I never did it. That changed this week. I’ve already added a couple of entries through the iPhone and expect to continue when I’m on the road or otherwise away from my desk.
The iPhone’s calendar program is simpler than iCal. Specifically, it doesn’t include the concept of multiple calendars, so any event you create on the iPhone is put into the general pool. When you sync, all the events created on the iPhone since the last sync are recognized and put into a single iCal calendar that you designate in iTunes. For me, that’s the “from-phone” calendar. I don’t want to put them all into “home” or “work,” because I’ll be adding events to both those categories.
So after a sync, I have a set of new events in this otherwise-useless “from-phone” calendar. To move them into the right calendars, I run a script called “Import from Phone.” It goes through every event in the “from-phone” calendar and asks me whether I want to reassign it to “home” or “work” (the “Neither” option leaves the event untouched--it’s an escape hatch if I decide I ran the script by mistake). By running this script instead of changing the events by hand, I not only save time, but also ensure that I don’t skip any of the new events from the iPhone.
Here’s the script:
1: tell application "iCal" 2: set phone to every event in calendar "from-phone" 3: repeat with i in phone 4: set summ to summary of i 5: set sdate to start date of i 6: set sday to short date string of sdate 7: display dialog ("Move " & quote & summ & quote & " on " & sday & " to:") buttons ["Home", "Work", "Neither"] default button 1 8: if button returned of result = "Home" then 9: make new event at end of events of calendar "home" with data i 10: delete i 11: else if button returned of result = "Work" then 12: make new event at end of events of calendar "work" with data i 13: delete i 14: end if 15: end repeat 16: reload calendars 17: end tell
Lines 4-6 get the identifying information from each event, and Line 7 includes that info in the dialog box. If you’re wondering why Lines 5 and 6 couldn’t be reduced to a single line like
set sday to short date string of (start date of i)
join the club. That’s how I first wrote the script, but it wouldn’t run because of some class error that made no sense to me. And no, adding more parentheses, like
set sday to (short date string of (start date of i))
didn’t help. Just another in a long list of things that make me think I’ll never really understand AppleScript.
As you can see from Lines 9-10 and 12-13, the script really doesn’t move the events into a different calendar. Instead, it copies the event data into a new event in the appropriate calendar, then deletes the old event. I looked in the iCal AppleScript dictionary but didn’t find a direct way to reassign an event to a different calendar, so this was my workaround.
reload calendars command on Line 16 refreshes the iCal window. Without it, it will look like the “from-phone” events were deleted without creating the “home” and “work” events. Yes, I learned that the hard way. The “home” and “work” events were there--they would appear if I switched to a different view--but hidden. The first time the script ran successfully I was sure I’d just deleted several events.
I’m sure I’ll be writing more about the iPhone as I learn how to use it.