Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This will allow you to select a ToDo then schedule an Event in Apple Calendar so you can block off time to undertake the task.
##############################################
# Title: Things3 To Calendar - Time Block
##############################################
# Iain Dunn
# Logic2Design
# www.logic2design.com
# Last update: 2021-06-06
# Version: 3
# Contributors and sources
# Michael Kummer michaelkummer.com
##############################################
# Configuration
##############################################
set the Calender_Lists to {"Personal", "Work", "Business", "Google"}
set defaultCalendar to "Personal"
##############################################
# Code
##############################################
set defaultReminderTime to "9"
set defaultReminder to "Today"
set now to (current date)
set today to now - (time of now)
set tomorrow to (today) + (24 * 60 * 60)
set thisWeek to (current date) + (7 * days)
set the Calender_Lists to {"Personal", "Work", "Business", "Google"}
tell application "Things3"
set scheduleList to {}
#Choose the type of To Do to Schedule
set theButton to button returned of (display dialog "Do you want to create an event for a ToDo due today, this week or all Open" with title "Select ToDo" buttons {"Today", "Due This Week", "All Open"} default button 1)
if theButton is "Today" then
set inboxToDos to every to do of list "Today" --or every to do whose status is open and due date is less than tomorrow
else if theButton is "Due This Week" then
set inboxToDos to (every to do whose status is open and due date is less than thisWeek) & (every to do of list "Today")
else
set inboxToDos to every to do whose status is open
end if
# Select the To Do
set scheduleToDo to {}
repeat with r from 1 to length of inboxToDos
set this_todo to item r of inboxToDos
set todoName to (get name of this_todo) as text
if scheduleToDo does not contain todoName then copy (todoName) to end of scheduleToDo -- removes duplicates
end repeat
if scheduleToDo is not {} then
set todoList to choose from list scheduleToDo with prompt "Select the ToDo to Schedule"
else
display dialog "There are no ToDos to Schedule" buttons {"OK"}
return
end if
if todoList is false then return
set todoSelect to every to do whose name is equal to todoList
repeat with r from 1 to length of todoSelect
set this_todo to item r of todoSelect
set eventName to (get name of this_todo) as text
set eventTags to (get tag names of this_todo) as text
set eventURL to (get id of this_todo) as text
set eventDue to (get due date of this_todo) as text
set eventNotes to (get notes of this_todo) as text
end repeat
tell me to activate
# Set Followup Date/Time
(choose from list {"2 Hours", "Today", "Tonight", "Tomorrow", "2 Days", "3 Days", "4 Days", "5 Days", "6 Days", "1 Week", "Saturday", "Sunday", "Next Monday"} default items defaultReminder OK button name "Select" with prompt "Set Event time" with title "Time Block ToDo")
set reminderDate to result as text
# exit if user clicks Cancel or Escape
if reminderDate is "false" then return
# for all the other options, calculate the date based on the current date
--set remindMeDate to my chooseRemindMeDate(reminderDate)
set theStartDate to my chooseRemindMeDate(reminderDate)
if reminderDate is "2 Hours" then
set defaultReminderTime to 0
else if reminderDate is "Tonight" then
defaultReminderTime
else
set defaultReminderTime to text returned of (display dialog "what time do you want to start? (answer in decimal ie 2:30pm is 14.5)" default answer "9")
end if
--set defaultReminderTime to text returned of (display dialog "what time do you want to start? (answer in decimal ie 8:30 is 8.5)" default answer "9")
# set the time for on the reminder date
if reminderDate is "2 Hours" then
set theStartDate to (current date) + 2 * hours
else if reminderDate is "Tonight" then
set time of theStartDate to 60 * 60 * 17
else
set time of theStartDate to 60 * 60 * defaultReminderTime
end if
display dialog "How long is the Event? (minutes)
Set to 0 for an all day event" default answer 30
set appt_length to text returned of result
if appt_length < 1 then
set appt_mins to (0)
set theEndDate to theStartDate
set allDay to true
else
set appt_mins to (appt_length)
set theEndDate to theStartDate + (appt_mins * minutes)
set allDay to false
end if
(choose from list Calender_Lists OK button name "Select" with prompt "Pick Calendar" with title "Create Calendar Event")
set Cal to result as text
tell application "Calendar"
tell calendar Cal
make new event with properties {summary:"Things3 - " & eventTags & " - " & eventName, start date:theStartDate, end date:theEndDate, description:"Due Date -" & eventDue & return & eventNotes, url:"things:///show?id=%22" & eventURL & "%22", allday event:allDay}
end tell
end tell
end tell
#################################################################################
# Functions
#################################################################################
# date calculation with the selection from the dialogue
# use to set the initial and the re-scheduled date
on chooseRemindMeDate(selectedDate)
if selectedDate = "2 Hours" then
set remindMeDate to (current date) + 0 * days
else if selectedDate = "Tonight" then
set remindMeDate to (current date) + 0 * days
else if selectedDate = "Today" then
set remindMeDate to (current date) + 0 * days
else if selectedDate = "Tomorrow" then
set remindMeDate to (current date) + 1 * days
else if selectedDate = "2 Days" then
set remindMeDate to (current date) + 2 * days
else if selectedDate = "3 Days" then
set remindMeDate to (current date) + 3 * days
else if selectedDate = "4 Days" then
set remindMeDate to (current date) + 4 * days
else if selectedDate = "5 Days" then
set remindMeDate to (current date) + 5 * days
else if selectedDate = "6 Days" then
set remindMeDate to (current date) + 6 * days
else if selectedDate = "1 Week" then
set remindMeDate to (current date) + 7 * days
else if selectedDate = "Saturday" then
# get the current day of the week
set curWeekDay to weekday of (current date) as string
if curWeekDay = "Monday" then
set remindMeDate to (current date) + 5 * days
else if curWeekDay = "Tuesday" then
set remindMeDate to (current date) + 4 * days
else if curWeekDay = "Wednesday" then
set remindMeDate to (current date) + 3 * days
# if it's Thursday, I'll set the reminder for Friday
else if curWeekDay = "Thursday" then
set remindMeDate to (current date) + 2 * days
# if it's Friday I'll set the reminder for Thursday next week
else if curWeekDay = "Friday" then
set remindMeDate to (current date) + 1 * days
else if curWeekDay = "Saturday" then
set remindMeDate to (current date) + 7 * days
else if curWeekDay = "Sunday" then
set remindMeDate to (current date) + 8 * days
end if
else if selectedDate = "Sunday" then
# end of week means Sunday in terms of reminders
# get the current day of the week
set curWeekDay to weekday of (current date) as string
if curWeekDay = "Monday" then
set remindMeDate to (current date) + 6 * days
else if curWeekDay = "Tuesday" then
set remindMeDate to (current date) + 5 * days
else if curWeekDay = "Wednesday" then
set remindMeDate to (current date) + 4 * days
# if it's Thursday, I'll set the reminder for Friday
else if curWeekDay = "Thursday" then
set remindMeDate to (current date) + 3 * days
# if it's Friday I'll set the reminder for Thursday next week
else if curWeekDay = "Friday" then
set remindMeDate to (current date) + 2 * days
else if curWeekDay = "Saturday" then
set remindMeDate to (current date) + 1 * days
else if curWeekDay = "Sunday" then
set remindMeDate to (current date) + 7 * days
end if
else if selectedDate = "Next Monday" then
set curWeekDay to weekday of (current date) as string
if curWeekDay = "Monday" then
set remindMeDate to (current date) + 7 * days
else if curWeekDay = "Tuesday" then
set remindMeDate to (current date) + 6 * days
else if curWeekDay = "Wednesday" then
set remindMeDate to (current date) + 5 * days
else if curWeekDay = "Thursday" then
set remindMeDate to (current date) + 4 * days
else if curWeekDay = "Friday" then
set remindMeDate to (current date) + 3 * days
else if curWeekDay = "Saturday" then
set remindMeDate to (current date) + 2 * days
else if curWeekDay = "Sunday" then
set remindMeDate to (current date) + 1 * days
end if
end if
return remindMeDate
end chooseRemindMeDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment