Skip to content

Instantly share code, notes, and snippets.

@thpani
Created May 3, 2011 08:57
Show Gist options
  • Save thpani/953037 to your computer and use it in GitHub Desktop.
Save thpani/953037 to your computer and use it in GitHub Desktop.
### CONFIGURATION ###
global theTodoCalendar, theEventCalendar, eventPrefix
set theTodoCalendar to "Uni To Do"
set theEventCalendar to "Uni"
set eventPrefix to "DUE: "
### PROGRAM, don't modify! ###
to joinList(aList)
set retVal to ""
tell application "iCal"
repeat with theEvent in aList
set retVal to retVal & "\n- " & theEvent's start date
end repeat
end tell
end joinList
to getEvents(aSummary)
tell application "iCal"
tell calendar theEventCalendar
return every event whose summary is equal to eventPrefix & aSummary
end tell
end tell
end getEvents
tell application "iCal"
tell calendar theTodoCalendar
repeat with theTodo in every todo
set theEventsList to my getEvents(theTodo's summary)
set numEvents to theEventsList's length
if numEvents = 0 then
# no matching event found
if theTodo's due date is not missing value and theTodo's completion date is missing value then
set dueDate to theTodo's due date
display alert "Creating event for todo '" & theTodo's summary & "' on " & dueDate's short date string & "."
tell application "iCal"
tell calendar theEventCalendar
make new event at end with properties {summary:eventPrefix & theTodo's summary, start date:dueDate, allday event:true, url:"mailitem:" & theTodo's uid & "?type=todo&action=showparent"}
end tell
end tell
end if
else if numEvents = 1 then
# found a matching event
if theTodo's due date is not missing value and theTodo's completion date is not missing value then
display alert "Deleting event for completed todo '" & theTodo's summary & "'."
set theEvent to item 1 of theEventsList
tell application "iCal"
tell calendar theEventCalendar
delete theEvent
end tell
end tell
end if
else
# more than one matching event found
display alert "WUAAAA!!!\nFound more than one event for todo '" & theTodo's summary & "':" & my joinList(theEventsList)
end if
end repeat
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment