Using the existing defer date in a script

My knowledge in Applescript is very limited, but I thought I would be able to take this existing line in a script:

set _date to current date

and change it to set _date to the action’s existing defer date instead of the current date, but I get different error messages when I try. Can anyone tell me what the line should look like?

Can you share the whole script? Your method of creating the task object matters.

I am using this script:
Defer to tomorrow

I took a look at that script. I’m not sure you want to edit that line. You’re talking about the line in midnightTomorrow, right? Why would you want to change it?

This script lets me set the defer date to tomorrow, or to a date further into the future if I change the ”+ 1” to a higher number. What I am trying to do is to figure out if I could modify the script to add one or more days to existing future defer dates, but it sounds like I am on the wrong path. I thought it would be easy to take an existing defer date in an action and add a specified number of days to it.

this might be a start for what you’re looking for

--this is the default time for items deferred to tomorrow, 5am
set myStart to 5
set startTime to (myStart * hours)
set todaysDate to current date
set currentTime to time of todaysDate
--this sets up tomorrow as 5am tomorrow
set tomorrowsDate to todaysDate - currentTime + (1 * days) + startTime
set deferDays to "1"

display dialog "how many days?" default answer deferDays buttons {"Cancel", "OK"} default button 2
set deferDays to (the text returned of the result) as integer

tell application "OmniFocus"
	tell front window
		set selectedTrees to selected trees of content
		set selectedTasks to every item of selectedTrees
		-- Set the defer date of each selected task to the startDate
		repeat with i from 1 to count of selectedTasks
			set theTask to the value of item i of selectedTasks
			set currentDefer to defer date of theTask
			if todaysDate > currentDefer then
				--if a defer date is in the past, defer to tomorrow at myStart
				set the defer date of theTask to tomorrowsDate
				if currentDefer is not missing value then
					set currentTime to time of currentDefer --gets the current time of the defer date
					set the defer date of theTask to currentDefer - currentTime + startTime + (deferDays * days) --advances the defer date by one day
					set the defer date of theTask to tomorrowsDate
				end if
			end if
		end repeat
	end tell
end tell
1 Like

Very embarrassing… I hadn’t the time to look at this immediately. Then I forgot it as I started to use Omnifocus differently, with less need for defer dates. Now, eight months later later, I tried this script and liked it. Thank you, @hammer, for taking the time to write it. It seems to work well for tasks with future defer dates (for tasks without defer dates it sets tomorrow as defer date, regardless of what number of days I type in the dialog box). It’s definitely, as you suggested, a start for what I was looking for, and still would find quite useful. From that start I could experiment and hopefully understand enough to make some changes that suit my needs. Thanks.