View Single Post
Quote:
Originally Posted by RobTrew View Post
The list comes out unsorted. This could be fixed by querying the cache rather than the applescript library, and using an ORDER BY statement in the SQL.
To get a sorted list without resorting to SQL and the cache, you could, of course, simply generate an additional column containing a sortable (numeric) form of the date, and then get the shell to do the sorting and remove the temporary column with something like:

Code:
echo reportlines | sort -k 1,3 | cut -f 2,3"
(sort by columns 1 and 3, then return only columns 2 and 3)
e.g.

Code:
property pstrSubject : "TPS Report"
property pstrGreeting : "Boss Name"
property pstrThisWeekHdr : "Here's what I did this week:"
property pstrNextWeekHdr : "Here's what I plan on doing next week:"
property pstrRecpName : "Boss"
property pstrRecpAddr : "the_boss@megacorp.com"

property dteNow : current date
property dteHistory : dteNow - (7 * days)
property dteFuture : dteNow + (7 * days)
property pdteBase : missing value

on run
	-- MAKE SURE THAT WE HAVE A BASE DATE FOR EXPRESSING PARTICULAR DATES AS SORTABLE INTEGERS
	if pdteBase is missing value then
		set pdteBase to dteNow
		tell pdteBase
			set {its year, its month, its day, its time} to {2001, 1, 1, 0}
		end tell
	end if
	
	tell application "OmniFocus"
		tell front document
			-- DEFINE THE TWO SETS OF TASKS,
			set refHistory to a reference to (flattened tasks where (in inbox = false and its modification date > dteHistory))
			set refFuture to a reference to (flattened tasks where 
				(in inbox = false) and (completion date is missing value) and (modification date < dteHistory) 
				and (due date > dteHistory) and (due date < dteFuture))
			
			-- COLLECT THEIR NAMES AND THE DATES REQUIRED,
			set {lstHDate, lstName} to {modification date, name} of refHistory
			repeat with i from 1 to length of lstHDate
				set dte to item i of lstHDate
				set item i of lstHDate to ((dte - pdteBase) as string) & tab & short date string of dte & tab & item i of lstName
			end repeat
			
			set {lstFDate, lstName} to {due date, name} of refFuture
			repeat with i from 1 to length of lstFDate
				set dte to item i of lstFDate
				set item i of lstFDate to ((dte - pdteBase) as string) & tab & short date string of dte & tab & item i of lstName
			end repeat
		end tell
	end tell
	
	-- AND SORT THE LISTS.
	-- (setting line delimiter to \n in preparation for sorting lines in the shell)
	set text item delimiters to "
"
	set strThisWeek to do shell script "echo " & quoted form of (lstHDate as string) & " | sort -k 1,3 | cut -f 2,3"
	set strNextWeek to do shell script "echo " & quoted form of (lstFDate as string) & " | sort -k 1,3 | cut -f 2,3"
	set my text item delimiters to space
	
	set strMsg to (pstrGreeting & "

" & pstrThisWeekHdr & "

" & strThisWeek) & "

" & pstrNextWeekHdr & "

" & strNextWeek
	
	tell application "Mail"
		tell (make new outgoing message with properties {visible:true, subject:pstrSubject, content:strMsg})
			make new to recipient at end of to recipients with properties {name:pstrRecpName, address:pstrRecpAddr}
		end tell
		activate
	end tell
end run
--

Last edited by RobTrew; 2011-02-07 at 03:46 AM.. Reason: Allowed for use of due date in future list