Allowing the user to enter a relative date

A simple approach (which I find quite useful) to getting a relative date from the user:

property pTitle : "Get a relative date from the user"
property pPrompt : "Enter a relative date in the form:

	today +/-  N * days
or
	now +/- N * days
or
	yesterday / tomorrow
"
property pDefn : "set Now to current date
set Today to Now - (time of Now)
set Tomorrow to Today + 1 * days
set Yesterday to Today - 1 * days
"
property pExample : "today + 7 * days"

set dteChosen to missing value
repeat while dteChosen is missing value
	set strWhen to text returned of (display dialog pPrompt default answer pExample with title pTitle)
	-- IF THE DATE IS ABSOLUTE AND DIRECTLY PARSEABLE BY THE DATE FUNCTION, NO PROBLEM ...
	try
		set dteChosen to date strWhen
	end try
	
	-- OTHERWISE TRY TO EVALUATE IT AS A RELATIVE EXPRESSION WITH RUN SCRIPT ...
	if dteChosen is missing value then
		-- (check the string before it is passed to run script)
		if (strWhen does not contain "--") and ¬
			(strWhen contains "now" or strWhen contains "today" or strWhen contains "yesterday" or strWhen contains "tomorrow") then
			try
				set dteChosen to run script (pDefn & strWhen)
			on error strMsg
				display dialog strMsg buttons {"OK"} with title pTitle
			end try
		else
			display dialog quoted form of strWhen & return & return & "does not appear to be a relative date" buttons {"OK"} with title pTitle
		end if
	end if
end repeat

display dialog dteChosen as string buttons {"OK"} with title pTitle