AppleScript to Populate GeekTool List on Desktop

I thought to share this script for general consumption. I pull tasks form various perspectives and put them in a display on my desktop.

(*
	This script creates a list of actions in a given perspective
		version 6.1 fixed repeat case to avoid action list count of zero
            ...
		version 1.0 (JJW)
*)

-- properties

-- allow input of top goals
-- show items due from Forecast (at top)
-- how many days to call "Due Soon"
-- show a Flag on items that are "Over Due"
-- show Forecast items that are still deferred

property pInputGoals : false
property pTopGoals : "" -- global storage for goals

property pShowForecast : false
property pDueSoonDays : 4
property pOverDueFlag : "❗️"
property pShowOnHoldContexts : false
property pShowDeferredDue : false

-- perspectives to display (in addition to Forecast)

property pName : {"Do First", "Waiting On", "Bills"}

-- show zero item lists?

property pShowZero : true

-- items will be displayed with a pBullet prefix for example ...
--property pBullet : "◉" & tab

property pBullet : ""
property pMissingContext : ">"

-- set flags to show context and only show first (special) character of context

property pShowContext : true
property pShowFirstCharacterOnly : true
property pShowProject : true

-- show / sort on days since creation

property pShowDaysInBox : true
property pSortbyDaysInBox : false

-- items not in Forecast that have due dates use this flag

property pDueFlag : "❗️"

-- specifically include a flag on items EXCEPT this perspective

property pNoFlag : "Active"
property pFlag : "🚩 "

-- sort algorithms and handler
-- get these from http://mac.softpedia.com/get/Utilities/A-Dose-of-Sorts.shtml
-- install them properly

property fSorterPath : "z>others:handlers:A Dose of Sorts:Quicksort:CustomQsort.scpt"
property fSorter : (load script file ((path to scripts folder as text) & fSorterPath))

on isGreater(a, b)
	(a's td > b's td)
end isGreater

on isLesser(a, b)
	(b's td > a's td)
end isLesser

on run {}
	set theList to my getOFPerspectiveList(0)
	return theList
end run

on getOFPerspectiveList(renewGoals)
	
	-- set local variables
	
	set Report to ""
	set ActionList to {}
	--set TreeList to {}
	set theDateToday to the (current date)
	set shortDate to short date string of theDateToday
	set shortTime to time string of theDateToday
	
	set Report to shortDate & " " & shortTime & return
	
	-- get top goals
	
	if (renewGoals is not 0) then
		getTopGoals()
		set Report to Report & pTopGoals
	end if
	
	-- collect Forecast items
	
	if pShowForecast then
		tell application "OmniFocus"
			
			-- get Forecast perspective list
			
			tell front document window of default document to set its perspective name to "Forecast"
			
			tell content of front document window of default document to ¬
				set TreeList to (value of every leaf)
			
			-- transfer the action items into readable form
			
			repeat with ListItem in TreeList
				try
					if (due date of ListItem is not missing value) then
						if (defer date of ListItem is not missing value) then
							if (defer date of ListItem is less than today) then
								display dialog name of ListItem
							end if
						end if
						set theOFDue to due date of ListItem
						tell application "Finder" to set theDueDays to (theOFDue - theDateToday) / (60 * 60 * 24)
						if (theDueDays ≤ pDueSoonDays) then
							try
								if ((pShowOnHoldContexts is true) or (the allows next action of the context of ListItem is true)) then
									set theDueDays to (theDueDays as integer)
									if (the context of ListItem is missing value) then
										set theContext to pMissingContext
									else
										set theContext to the name of the context of ListItem
									end if
									set end of ActionList to {td:round (theDueDays), tc:theContext, tp:name of containing project of ListItem, tn:name of ListItem}
								end if
							on error
								set end of ActionList to {td:round (theDueDays), tc:"📂", tp:name of containing project of ListItem, tn:name of ListItem}
							end try
						end if
						--end if
					end if
					--end if
				end try
				
			end repeat
		end tell
		
		-- sort the Forecast items
		
		tell fSorter to sort(ActionList, 1, -1, {comparer:isGreater})
		repeat with ListItem in ActionList
			if (ListItem's td < 0) then
				set theTD to ListItem's td
				set ListItem's td to pOverDueFlag & theTD
			end if
		end repeat
		
		-- put Forecast perspective in Report
		
		if (((count of ActionList) is 0) and pShowZero) or ((count of ActionList) is not 0) then
			set Report to Report & "Forecast" & " [" & (count of ActionList) & "]" & return & "--" & return
			repeat with ListItem in ActionList
				if pShowContext then
					if (pShowFirstCharacterOnly) then
						set pc to first character of tc of ListItem & " : "
					else
						set pc to tc of ListItem & " : "
					end if
				else
					set pc to ""
				end if
				if pShowProject then
					set pp to tp of ListItem & " - "
				else
					set pp to " - "
				end if
				set tdShow to ListItem's td & " "
				set Report to Report & pBullet & tdShow & pc & pp & tn of ListItem & return as string
			end repeat
			set Report to Report & "==" & return
		end if
	end if
	
	-- collect all other perspectives
	
	repeat with PItem in pName
		
		set ActionList to {}
		
		tell application "OmniFocus"
			
			tell front document window of default document to set its perspective name to PItem
			
			tell content of front document window of default document to set TreeList to (value of every leaf)
			
			-- transfer the action items into readable form
			repeat with ListItem in TreeList
				set isDueFlag to ""
				if (ListItem is not in inbox) then
					if (parent task of ListItem is missing value) then
						if (due date of ListItem is not missing value) then set isDueFlag to pDueFlag
					else
						if (due date of parent task of ListItem is not missing value) then set isDueFlag to pDueFlag
					end if
				else
					if (due date of ListItem is not missing value) then set isDueFlag to pDueFlag
				end if
				if (((flagged of ListItem is true) or (flagged of parent task of ListItem is true)) and (PItem as text is not equal to pNoFlag as text)) then
					set isDueFlag to pFlag & isDueFlag
				end if
				set theDaysInBox to ""
				if (pShowDaysInBox) then
					if (isDueFlag is pDueFlag) then
						if (the due date of ListItem is not missing value) then
							set itsModDate to the (due date of ListItem)
						else
							set itsModDate to the (due date of parent task of ListItem)
						end if
					else
						set itsModDate to the modification date of ListItem
					end if
					tell application "Finder" to set theDaysInBox to ¬
						-1 * (((theDateToday - itsModDate) / (60 * 60 * 24)) as integer)
				end if
				-- put isDueFlag temporarily in tib
				--display dialog (PItem & ":" & name of ListItem as string)
				if (ListItem is in inbox) then
					set containerName to "Inbox"
				else
					set containerName to name of containing project of ListItem
				end if
				set end of ActionList to ¬
					{td:theDaysInBox, tc:name of context of ListItem, tp:containerName, tn:name of ListItem, tib:isDueFlag}
			end repeat
			if (pSortbyDaysInBox and pShowDaysInBox) then
				tell fSorter to sort(ActionList, 1, -1, {comparer:isGreater})
			end if
			repeat with ListItem in ActionList
				set ListItem's td to ListItem's tib & ListItem's td & " "
			end repeat
		end tell
		
		-- output the action items in Report
		
		if (((count of ActionList) is 0) and pShowZero) or ((count of ActionList) is not 0) then
			set Report to Report & PItem & " [" & (count of ActionList) & "]" & return & "--" & return
			if ((count of ActionList) is not 0) then
				repeat with ListItem in ActionList
					if pShowContext then
						set pc to first character of tc of ListItem & " : "
					else
						set pc to ""
					end if
					set pp to " - "
					if pShowProject then
						set pp to tp of ListItem & " - "
					end if
					set Report to Report & pBullet & ListItem's td & pc & pp & ListItem's tn & return as string
				end repeat
			end if
			--end tell
			set Report to Report & "==" & return
		end if
		
	end repeat
	tell application "OmniFocus"
		tell front document window of default document to set its perspective name to "Forecast"
	end tell
	Report
end getOFPerspectiveList

on getTopGoals()
	try
		display dialog "Enter your top three goals for today with ;" default answer ""
	on error
		return
	end try
	set theGoals to the text returned of the result
	set cD to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ";"
	set theGoals to the text items of theGoals
	set AppleScript's text item delimiters to cD
	set theDate to current date
	
	set myDay to "--" & return
	repeat with theGoal in theGoals
		set myDay to myDay & "-> " & theGoal & return
	end repeat
	
	set myDay to myDay & "--" & return
	
	set pTopGoals to myDay
	
	return pTopGoals
	
end getTopGoals

Enjoy!


JJW