Code:
--CreateCalendar
--This OmniFocus script takes the tasks in the currently displayed window and creates or refreshes
--an iCal calendar with those tasks.
--Copyright 2007 Preston Holmes
--preston@ptone.com
-- v1.0 12/20/2007
--v1.01 selected last minute bug in testing for cancel button
--set this pref to not be prompted for a calendar name
--(default is to base calendar name on OF window with confirmation)
set calName to "School Tasks"
--set this pref to false to not be asked you choices on the following
set promptPrefs to false
set shouldMakeStartDate to false -- create a calendar event for a task start date
set shouldMakeDueDate to true -- create a calendar event for a task due date
set shouldMakeDatedTaskTodos to false -- create iCal todos for events that have start of due dates
set shouldMakeUnDatedTaskTodos to false -- create iCal todos for events that do not have start or due dates
set shouldMakeAsAllDay to false -- create calendar events as all day events
set labelPref to "prefix" --set to prefix to have events labeled "Due:[task name]" or suffix for "[task name] (Due)"
tell application "OmniFocus"
--get calendar name if not preset
if calName = "" then
set winName to get name of window 1
if winName starts with "Focusing" then
set calName to characters 13 thru -1 of winName as string
else if winName starts with "Perspective" then
set calName to perspective name of window 1
else
set calName to "OmniFocus"
end if
try
display dialog "Calendar Name:" default answer calName
set calName to text returned of result
on error
return
end try
end if
--get user prefs if prompt is enabled
if promptPrefs then
set prefChoices to {"Create Events for Start Dates", "Create Events for Due Dates", "Create ToDos for tasks that have dates", "Create ToDos for tasks that do not have dates", "Create Events as All Day Events"}
set prefs to {a reference to shouldMakeStartDate, a reference to shouldMakeDueDate, a reference to shouldMakeDatedTaskTodos, a reference to shouldMakeUnDatedTaskTodos, a reference to shouldMakeAsAllDay}
choose from list prefChoices ¬
with title ¬
"Preferences" with prompt ¬
"Configure Calendar (cmd click for multiple)" OK button name ¬
"Create Calendar" with multiple selections allowed
set selectedPrefs to result
if selectedPrefs = false then return --cancel button
repeat with i from 1 to count of prefChoices
if selectedPrefs contains item i of prefChoices then
set contents of item i of prefs to true
end if
end repeat
if not (shouldMakeStartDate or shouldMakeDueDate or shouldMakeDatedTaskTodos or shouldMakeUnDatedTaskTodos) then
display alert "You have not chosen to create anything for this calendar, choose at least on preference that Creates an event or ToDo"
return
end if
end if
--gather tasks from OF
set tasklist to value of every leaf of content of document window 1 of default document
--Initialize the calendar
tell application "iCal"
try
set targetCal to (the first calendar whose title is calName)
on error errMsg number errNum
log errMsg
log errNum
set targetCal to make new calendar with properties {title:calName}
end try
--this is a wipe and restore type operation
delete events of targetCal
delete todos of targetCal
end tell
--Main loop, for each task create an event and/or Todo
repeat with aTask in tasklist
--get task attributes
set taskID to id of aTask
set tURL to "omnifocus:///task/" & taskID
set taskName to name of aTask
set taskNote to note of aTask
set dueDate to due date of aTask
set startDate to start date of aTask
if shouldMakeStartDate and (startDate ≠ missing value) then
if labelPref = "prefix" then
set eText to "Start: " & taskName
else
set eText to taskName & " (Start)"
end if
tell application "iCal"
set theEvent to make event at end of events of targetCal with properties ¬
{start date:startDate, end date:startDate, summary:eText, description:taskName & ": " & taskNote, allday event:shouldMakeAsAllDay, url:tURL}
end tell
end if
if shouldMakeDueDate and (dueDate ≠ missing value) then
if labelPref = "prefix" then
set eText to "Due: " & taskName
else
set eText to taskName & " (Due)"
end if
tell application "iCal"
set theEvent to make event at end of events of targetCal with properties ¬
{start date:dueDate, end date:dueDate, summary:eText, description:taskName & ": " & taskNote, allday event:shouldMakeAsAllDay, url:tURL}
end tell
end if
if ((startDate ≠ missing value or dueDate ≠ missing value) and shouldMakeDatedTaskTodos) then
tell application "iCal"
make new todo at end of todos of targetCal with properties ¬
{summary:taskName, description:taskName & ": " & taskNote, due date:dueDate, url:tURL}
end tell
else if shouldMakeUnDatedTaskTodos then
tell application "iCal"
make new todo at end of todos of targetCal with properties ¬
{summary:taskName, description:taskName & ": " & taskNote, url:tURL}
end tell
end if
end repeat
end tell
tell application "iCal" to activate