I’m trying to write a script that will check a series of files called “Loose Ends” where I append questions to ask my professors.
If the file is empty, that means there are no outstanding questions and all is well. But if I have a question, I want my script to schedule a To-Do for the next available office hours.
I don’t think the file directories are the problem, as all are standardized like this -
Macintosh HD:Users:mpa15:Desktop:Spring 2011:Biochem:_Biochem Loose Ends.txt
For some reason the script gets stuck on the dueDate section. Any help would be greatly appreciated. Thanks in advance.
set theSubjectList to {"Biochem", "Phys", "Mol Phys", "Phys Lab", "Neuro"}
set theOfficeHours to {"Monday", "Wednesday", "Wednesday", "Thursday", "Thursday"}
set theNumberedList to {1, 2, 3, 4, 5}
on DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(d, w, i) -- returns a date
-- Keep an note of whether the instance value *starts* as zero
set instanceIsZero to (i is 0)
-- Increment negative instances to compensate for the following subtraction loop
if i < 0 and d's weekday is not w then set i to i + 1
-- Subtract a day at a time until the required weekday is reached
repeat until d's weekday is w
set d to d - days
-- Increment an original zero instance to 1 if subtracting from Sunday into Saturday
if instanceIsZero and d's weekday is Saturday then set i to 1
end repeat
-- Add (adjusted instance) * weeks to the date just obtained and zero the time
d + i * weeks - (d's time)
end DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate
set today to current date
set theWeekDay to the weekday of today
repeat with theNumber in theNumberedList
set theSubject to item theNumber of theSubjectList
set theOfficeHour to item theNumber of theOfficeHours
set theFolder to POSIX path of (the path to desktop)
set theFile to (POSIX file (theFolder & "/Spring 2011/" & theSubject & "/_" & theSubject & " Loose Ends.txt"))
open for access theFile
set fileContents to (read theFile)
close access theFile
if fileContents contains "?" then
set dueDate to current date
if theWeekDay is not theOfficeHour then
DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(today, theOfficeHour, 1)
set dueDate to result as date
end if
tell application "Things"
set newToDo to make new to do with properties {name:"Visit Office Hours"}
set toDoToSchedule to first to do of list "Inbox"
schedule toDoToSchedule for dueDate
end tell
end if
end repeat