Update defer dates to always be current date, then delete once it reaches due date

Hi all,

I primarily use the forecast tab, and especially the Today frame, to manage my workflow. I use defer dates as when I can start working on a task, and due dates as when that task needs to be finished by. I want to see all the due today tasks at the top, then deferred tasks (aka ones that I can be working on) underneath that.

The way OF2 is built, the Today frame shows first tasks that are due today, then underneath that tasks that have been deferred to today. This means that a task with the same due date and defer date will appear twice under Today. While it’s just cosmetic, I do find it distracting to have tasks “doubled up”. The worse part is that if a task has a defer date that isn’t today, it doesn’t appear under the Today section. So if a task has a defer date of December 2, I’ll see it in the Today frame on the 2nd but then it disappears again on the 3rd. Since I use defer dates as start dates, I need a task to always show up under Today once it reaches its defer date.

Therefore, I’ve been trying to make a script and automatically run it at midnight every day to deal with these issues. I need it to A) update any past defer dates to the current date. This way, once I reach the start date for a project it will always show up under the Today frame. I also need it to B) delete any task’s defer date once it reaches its due date. This way, a task won’t appear twice under the Today frame once it’s due.

I found a basic script online that started to get at what I want, but I’ve been building on top of it. It does successfully update past defer dates to the present date, but I can’t seem to make it delete the defer date once it matches the due date. Below is the latest version of code that compiles and runs without producing an error message. Can anyone tell me where I might be going wrong? Thanks in advance!

on run
	tell application "OmniFocus"
		set todayDate to current date
		set todayDate's hours to 0
		set todayDate's minutes to 0
		set todayDate's seconds to 0
		
		-- loop through all hours of day
		repeat with hours from 0 to 23
			--loop through all minutes of hour
			repeat with minutes from 0 to 59
				
				-- if a task's defer date is before today, update it to today's date
				tell default document
					set todayTasks to (flattened tasks where ((defer date < todayDate) and (completed is false)))
					repeat with t in todayTasks
						set t's defer date to todayDate
					end repeat
				end tell
				
				-- if a task is due today, delete its defer date
				tell default document
					set dueTasks to (flattened tasks where (due date is equal to todayDate) and (completed is false))
					repeat with t in dueTasks
						set t's defer date to missing value
					end repeat
				end tell
				
			end repeat
		end repeat
		
	end tell
end run

I had a quick response to say that marking a date as missing value will delete it. But, then I see that you already do this.

One thought is that you are trying to match your due date in precision increments of seconds or greater. Rather than using “due date is equal to todayDate”, try finding due dates that are not greater than todayDate AND not less than todayDate. See also a trick below.

I also suggest that your loops are overly laborious in design. Try this perhaps …

  • Collect all tasks that have due and defer dates. Something akin to this …

set tasksToSet to flattened tasks where ((its due date is not missing value) and (its defer date is not missing value) and (its completed is false))

OR

set tasksToSet to flattened task with {due date: not missing value, defer date: not missing value, completed: false} – untested

  • Loop through this set ONLY …

repeat with theTask in …
if theTask’s defer date < todayDate then set theTask’s defer date to todayDate
if (not theTask’s due date is not todayDate) then set theTask’s defer date to missing value
end repeat

Hope this gives some correct insight.

–
JJW

JJW or theguy02,

Would either of you mind posting your final version of the script? I am really interested in this. Also, I haven’t created a script yet. Do you know of a good forum entry to help with that?

Did you ever figure out how to delete the defer date when the due date arrives? I really want that. Thanks!

Tripp

Maybe this helps. It does what is requested in this post.Tell me if it works for you.

Code:

--unlocked2412
set dateNow to (current date)
tell dateNow
	set its hours to 0
	set its minutes to 0
end tell

tell application "OmniFocus"
	tell front document
		-- if a task's defer date is before today, update it to today's date		
		set defer date of (every flattened task whose (defer date < dateNow) and (completed = false)) to dateNow
		-- if a task is due today, delete its defer date
		set defer date of (every flattened task whose (due date > dateNow) and (completed = false)) to missing value
	end tell
end tell
1 Like

That’s exactly what I need. Thanks so much.

Now I need to automate it. Should I use a Stay Open Script as suggested in this article - http://www.peachpit.com/blogs/blog.aspx?uk=Mac-Productivity-Scheduling-AppleScripts&WT.rss_f=AuthorBlog&WT.rss_a=Mac-Productivity-Scheduling-AppleScripts&WT.rss_ev=a?

If so, what’s a recommended cycle time? Thanks!

You’re welcome. I don’t think it’s necessary to have this script running all day. Maybe you can run an Automator Calendar Alarm at 8:00 for example.
Tell me if it works for you.