Applescript to create Due.app URLs

I have become a little enamoured with Sean Korzdorfer’s use of Due as a sidecar app to Omnifocus. He has some Applescripts and other pieces of code that make it very easy to work with, but I really enjoyed reading his Open Notebook which lead me to his Sequential Due Tasks. He is using the URL Scheme in Due.app to create tasks that cascade to the next. I love the idea, but have limited use, but it did get me thinking about how I can use Due in my workflow.

For me Due is the persistant reminder. I only put things in it that I really want to be reminded of, like days to take out the trash and recycling, a reminder in the morning to get headed to work, etc. But if I want to use it more tactically, I need a way to get items into it easier. Sean has a great Applescript to create Due tasks, but I want to be able to create a due:// URL from anywhere on my Mac. I cobbled together the script below, your mileage may vary.

set epoch to (date "Thursday, January 1, 1970 12:00:00 AM")
set theDate to text returned of (display dialog ("Due Date:") default answer "")
set theTime to text returned of (display dialog ("Due Time:") default answer "")
set theTask to text returned of (display dialog ("Task:") default answer "")
set totalDate to (date (theDate & " " & theTime))
set UnixTime to (totalDate - epoch) as integer
-- display dialog UnixTime
set theTextEnc to urlencode(theTask) of me
-- display dialog theTextEnc default answer theTextEnc
-- http://harvey.nu/applescript_url_encode_routine.html
on urlencode(theText)
set theTextEnc to ""
repeat with eachChar in characters of theText
set useChar to eachChar
set eachCharNum to ASCII number of eachChar
if eachCharNum = 32 then
set useChar to "%20"
else if (eachCharNum ? 42) and (eachCharNum ? 95) and (eachCharNum 46) and (eachCharNum 57) and (eachCharNum 90) and (eachCharNum 122) then
set firstDig to round (eachCharNum / 16) rounding down
set secondDig to eachCharNum mod 16
if firstDig > 9 then
set aNum to firstDig + 55
set firstDig to ASCII character aNum
end if
if secondDig > 9 then
set aNum to secondDig + 55
set secondDig to ASCII character aNum
end if
set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
set useChar to numHex
end if
set theTextEnc to theTextEnc & useChar as string
end repeat
return theTextEnc
end urlencode
-- http://www.macosxautomation.com/applescript/sbrt/sbrt-02.html
on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string
set dueURL to ("due://x-callback-url/add?title=" & theTextEnc & "&duedate=" & number_to_string(UnixTime) & "&timezone=GMT") as string
set the clipboard to dueURL