04 June 2009

Applescript to create Journler entries from all events in an iCal calendar

I searched long and hard on the you-know-what to find some nerd who had already done what I needed to do -- "import" all of my iCal events in a certain Calendar into Journler as events, also attaching the iCal event attendee to the Journler entry as a Contacts resource.

Surely someone else had done this already! I don't have to muck around with programming/scripting anymore, do I? I don't have to learn AppleScript all of a sudden; I mean, I'm sick of this shit, we're supposed to have robot servants to do things like this for us by now!

So I had to be my own nerd, and if you are the me of yesterday and are looking for a script to do this, you are welcome. Just use it at your own risk because I learned as little about AppleScript as I possibly could in order to throw this together and resented it every step of the way and wasn't careful with error handling and all that mess.

But it totally works! As a reference, I converted 158 entries this way with no problem, took a few minutes. I knew my mysterious past in data conversion would pay off some day.


-- place in ~/Library/Scripts/Journler
-- Creates Journler entries from all iCal events in the calendar named "Journler Drop" (copy desired events here)
-- Sets a tag of "iCal" to the Journler entry
-- In addition, if there is an attendee on the iCal event (just the last attendee, if there are multiple), it attaches a Contacts Resource to the Journler entry
-- Created by Courtney Lamb 6/4/09 (www.courtneylamb.com)
-- Use at your own risk!

tell application "iCal"
tell calendar "Journler Drop"
set theCount to count of events
-- Loop through all of the iCal events in the given calendar
repeat with j from 1 to theCount
set theEvent to item j of events
set theSummary to summary of theEvent
set theDate to start date of theEvent

-- Put both the iCal description and location in the Journler notes
set theNotes to ""
set theLocation to ""

if exists (description of theEvent) then
set theNotes to description of theEvent
else
set theNotes to ""
end if
if exists (location of theEvent) then
set theLocation to location of theEvent
else
set theLocation to ""
end if

-- Get only the last of the attendees, if any
set theContactID to ""
if exists (the last attendee of theEvent) then
set theName to display name of the last attendee of theEvent
tell application "Address Book"
if exists (the first person whose organization = theName) then
set thePerson to (the first person whose organization = theName)
set theContactID to id of thePerson
else
set theContactID to ""
end if
end tell
end if

-- Create the Journler entry
set theNewTag to {"iCal"}
set theCategory to "Contacts"
tell application "Journler"
set anEntry to make new entry
set the name of anEntry to theSummary
set the date created of anEntry to theDate
set the tags of anEntry to theNewTag
set the category of anEntry to theCategory
set the rich text of anEntry to theLocation & "
" & theNotes
-- Create the attached Contact resource from the iCal attendee
if theContactID is not "" then
set aResource to make new resource with properties {owner:anEntry, type:contact, contact id:theContactID}
end if
end tell
end repeat
end tell
end tell


No comments: