Create To Do's In Things

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

Hi, mpa15.

Your list ‘theOfficeHours’ contains text. The handler takes AppleScript weekday constants:

set theOfficeHours to {Monday, Wednesday, Wednesday, Thursday, Thursday} -- Not {"Monday", "Wednesday", "Wednesday", "Thursday", "Thursday"}

Other notes:
There’s no point in switching around between POSIX paths and POSIX files for the file reading.

There’s no need to open and close a file for access explicitly if you only want to read it once in the course of the script.

The coercion ‘as date’ doesn’t officially exist in AppleScript. The handler returns a date, so you don’t need a coercion anyway.

I assume that your use of ‘repeat with theNumber in theNumberedList’ instead of ‘repeat with theNumber from 1 to 5’ is deliberate.

set theSubjectList to {"Biochem", "Phys", "Mol Phys", "Phys Lab", "Neuro"}
set theOfficeHours to {Monday, Wednesday, Wednesday, Thursday, Thursday} -- Not {"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 (path to desktop as text)
	set theFile to (theFolder & "Spring 2011:" & theSubject & ":_" & theSubject & " Loose Ends.txt")
	set fileContents to (read file theFile as text)
	
	
	if fileContents contains "?" then
		set dueDate to current date
		
		if theWeekDay is not theOfficeHour then
			DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(today, theOfficeHour, 1)
			set dueDate to result
		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

Thanks Nigel! I am quite new to AppleScript, so all of the things you listed were done in ignorance and not purposely. I have taken all of your suggestions and the script works marvelously. Thanks again!