Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a task with subtasks for sending someone a birthday card.
(*
Creates a complex task to buy birthday cards.
You’ll probably want to customize createOmniFocusTasksForEventInProject()
with your own contexts, subtasks, and schedule of defer/due dates.
*)
on run
display dialog "Enter the event for the card:" default answer "Skylar’s Birthday on 10/2"
handle_string(the result's text returned)
end run
on handle_string(theString)
set theInfo to my parseString(theString)
set eventName to theInfo's what
set eventName to my replace_chars(eventName, "'", "")
try
set dueDate to when of (theInfo & {when:missing value})
on error -1728
set dueDate to missing value
end try
if dueDate is missing value then
set dueDate to my askForDueDate(eventName)
end if
my createTasksForEvent(eventName, dueDate)
end handle_string
on replace_chars(this_text, search_string, replacement_string)
set old_tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to old_tid
return this_text
end replace_chars
-- Parsing the input string
on parseString(theString)
if theString contains " on " then
return parseXonYString(theString)
end if
return {what:theString}
end parseString
on parseXonYString(theString)
set pos to offset of " on " in theString
if pos is 0 then
return {what:theString}
end if
set theTitle to text 1 through (pos - 1) of theString
set theDate to text (pos + 4) through -1 of theString
set theDate to my completeDate(theDate)
return {what:theTitle, when:theDate}
end parseXonYString
on completeDate(partialDate)
set fullDate to partialDate & "/" & (year of (current date))
set theCompleteDate to date fullDate
if theCompleteDate's month is less than month of (current date) then
set fullDate to partialDate & "/" & ((year of (current date)) + 1)
set theCompleteDate to date fullDate
end if
return theCompleteDate
end completeDate
-- Prompting for an explicit due date
on askForDueDate(eventName)
display dialog ("When is " & eventName & "?") default answer ""
end askForDueDate
-- Creating tasks
on createTasksForEvent(eventName, eventDate)
my createOmniFocusTasksForEventInProject(eventName, eventDate)
end createTasksForEvent
-- Create tasks in OmniFocus
on createOmniFocusTasksForEventInProject(eventName, eventDate)
set buyCardDate to eventDate - 21 * days
repeat while (buyCardDate's weekday is not Saturday)
set buyCardDate to buyCardDate - 1 * days
end repeat
set buyCardDate's hours to 17
tell application "OmniFocus"
tell default document
set errandsContext to first context whose name is "Errands"
set homeContext to first context whose name is "Home"
set proj to first flattened project whose name is "Family"
tell proj
set parentTask to make new task with properties {name:eventName, sequential:true, due date:eventDate}
tell parentTask
make new task with properties {name:"Buy a card for " & eventName, context:errandsContext, due date:buyCardDate}
make new task with properties {name:"Sign the card for " & eventName, context:homeContext, defer date:eventDate - (14 * days), due date:eventDate - (11 * days)}
make new task with properties {name:"Prepare " & eventName & " card for mailing", context:homeContext, due date:eventDate - (11 * days)}
make new task with properties {name:"Mail " & eventName & " card", context:errandsContext, due date:eventDate - (10 * days)}
end tell
end tell
end tell
end tell
end createOmniFocusTasksForEventInProject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.