View Single Post
Sorry to jump on this thread late, but I've had the following script running every hour for while now and it does a good job of giving me iCal events for OF tasks with due dates, and hence iPhone reminders. It differs from the others that I've seen by putting all tasks with a due date into a dedicated calendar (OF-Reminders, which you'll have to create before you run the script).

Script follows:


-- Creates iCal events for OmniFocus tasks
-- Events are created for 3 categories of task

-- 1 All tasks in the context named by morningContextName will have an event created the next day at morningContextTime
-- 2 Any task with a due date _and_ time will have an event created at that date and time
-- 3 Any task with a due date but no due time will have an all day event created

-- All events are created in the calendar named by targetCalendar, and with the alarm named by eventSoundName
-- The targetCalendar is cleared every run

-- If you synch targetCalendar with your iPhone over MobileMe this will give you iPhone notifications of due tasks


set morningContextName to "for work"
set morningContextTime to 7 * 3600
set eventSoundName to "Submarine"
tell application "iCal"
set targetCalendar to calendar "OF-Reminders"
tell targetCalendar
delete every event
end tell
end tell

tell application "OmniFocus"
my onTasks(inbox tasks of default document)
my onFolders(default document)
end tell

on onFolders(folderList)
using terms from application "OmniFocus"
repeat with eachFolder in folderList
onProjects(projects of eachFolder)
onFolders(folders of eachFolder)
end repeat
end using terms from
end onFolders

on onProjects(projectList)
using terms from application "OmniFocus"
repeat with eachProject in projectList
name of eachProject
onTasks(tasks of eachProject)
end repeat
end using terms from
end onProjects

on onTasks(aTaskList)
using terms from application "OmniFocus"
repeat with eachTask in aTaskList
onTask(eachTask)
onTasks(tasks of eachTask)
end repeat
end using terms from
end onTasks

on onTask(aTask)
using terms from application "OmniFocus"
if aTask is completed then
return
end if
set theContext to context of aTask
if theContext is not missing value then
set contextName to name of theContext
else
set contextName to missing value
end if
set taskDue to due date of aTask
set eventName to eventNameFor(aTask)
if taskDue is not missing value then
createEvent(eventName, id of aTask, taskDue)
else
if contextName is equal to my morningContextName then
set alarmTime to nextAlarmTime(my morningContextTime)
createEvent(eventName, id of aTask, alarmTime)
end if
end if
end using terms from
end onTask

on eventNameFor(aTask)
using terms from application "OmniFocus"
if containing project of aTask is missing value then
set projectName to "InBox"
else
set projectName to name of containing project of aTask
end if
if context of aTask is missing value then
set contextName to ""
else
set contextName to name of context of aTask
end if
return name of aTask & "(" & contextName & ":" & projectName & ")"
end using terms from
end eventNameFor

on createEvent(aSummary, anID, aDate)
tell application "iCal"
tell my targetCalendar
-- if (time of aDate) is equal to 0 then
-- return
-- end if
set existingEvents to every event whose (summary is equal to aSummary) and (start date is equal to aDate)
if existingEvents is not {} then return
if time of aDate is equal to 0 then
set newEvent to make new event at end with properties {summary:aSummary, start date:aDate, allday event:yes}
else
set newEvent to make new event at end with properties {summary:aSummary, start date:aDate, end date:aDate}
end if
if time of aDate is not equal to 0 then
tell newEvent
make new sound alarm at end with properties {trigger interval:0, sound name:my eventSoundName}
end tell
end if
end tell
end tell
end createEvent

on nextAlarmTime(timeSecs)
set now to current date
set alarmDay to now
if time of now is greater than my morningContextTime then
set day of alarmDay to (day of alarmDay) + 1
end if
set time of alarmDay to timeSecs
return alarmDay
end nextAlarmTime


-- Left for reference
on onContexts(contextList)
repeat with aContext in contextList
my onContext(aContext)
end repeat
end onContexts

on onContext(aContext)
using terms from application "OmniFocus"
onContexts(contexts of aContext)
onTasks(tasks of aContext)
end using terms from
end onContext