Skip to content

Instantly share code, notes, and snippets.

@dgreen
Last active December 31, 2018 04:08
Show Gist options
  • Save dgreen/2342860cbe335db46e38c88959206441 to your computer and use it in GitHub Desktop.
Save dgreen/2342860cbe335db46e38c88959206441 to your computer and use it in GitHub Desktop.
Completed Task Report
--==============================
-- OmniFocus > Completed Task Report
-- Version 1.1.0
-- Originally Written By: Ben Waldie <ben@automatedworkflows.com>
-- http://www.automatedworkflows.com
-- Description: This script retrieves a list of OmniFocus tasks completed today, yesterday, this week, last week, or this month. It then summarizes the tasks in a new Evernote note.
-- Version History:
-- 1.4.1 - DGG Add Quarterly Report
-- 1.3.0 - DGG Change folder, separately log items tagged Dropped
-- 1.2.0 - DGG update start weeks on Sunday
-- 1.1.0 - DGG Update for different title, different folder, fixed bug of disqualifying projects that were changed later than last week
-- 1.0.0 - Initial release
--==============================
-- Prompt the user to choose a scope for the report
activate
set theReportScope to choose from list {"Today", "Yesterday", "This Week", "Last Week", "This Month", "Last Quarter"} default items {"Last Week"} with prompt "Generate a report for:" with title "OmniFocus Completed Task Report"
if theReportScope = false then return
set theReportScope to item 1 of theReportScope
-- Calculate the task start and end dates, based on the specified scope
set theStartDate to current date
set hours of theStartDate to 0
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (23 * hours) + (59 * minutes) + 59
if theReportScope = "Today" then
set theDateRange to date string of theStartDate
else if theReportScope = "Yesterday" then
set theStartDate to theStartDate - 1 * days
set theEndDate to theEndDate - 1 * days
set theDateRange to date string of theStartDate
else if theReportScope = "This Week" then
repeat until (weekday of theStartDate) = Sunday
set theStartDate to theStartDate - 1 * days
end repeat
repeat until (weekday of theEndDate) = Saturday
set theEndDate to theEndDate + 1 * days
end repeat
set theDateRange to (date string of theStartDate) & " through " & (date string of theEndDate)
else if theReportScope = "Last Week" then
set theStartDate to theStartDate - 7 * days
set theEndDate to theEndDate - 7 * days
repeat until (weekday of theStartDate) = Sunday
set theStartDate to theStartDate - 1 * days
end repeat
repeat until (weekday of theEndDate) = Saturday
set theEndDate to theEndDate + 1 * days
end repeat
set theDateRange to (date string of theStartDate) & " through " & (date string of theEndDate)
else if theReportScope = "This Month" then
repeat until (day of theStartDate) = 1
set theStartDate to theStartDate - 1 * days
end repeat
repeat until (month of theEndDate) is not equal to (month of theStartDate)
set theEndDate to theEndDate + 1 * days
end repeat
set theEndDate to theEndDate - 1 * days
set theDateRange to (date string of theStartDate) & " through " & (date string of theEndDate)
else if theReportScope = "Last Quarter" then
set thePrompt to "What date would you like to use for the start of the quarter?"
set theReturnInput to text returned of (display dialog thePrompt default answer "")
if theReturnInput is equal to "" then
return
end if
set theStartDate to date (theReturnInput)
set theEndDate to theStartDate + (7 * 13 - 1) * days
set theDateRange to (date string of theStartDate) & " through " & (date string of theEndDate)
end if
-- Begin preparing the task list as HTML.
set theProgressDetail to "<html><body><h1>Completed Tasks</h1><br><b>" & theDateRange & "</b><br><hr><br>"
set theDroppedTasks to ""
-- Retrieve a list of projects modified within the specified scope
set modifiedTasksDetected to false
tell application "OmniFocus"
tell front document
-- Get a reference to the Dropped tag
try
set droppedTag to first tag where its name contains "Dropped"
on error
display alert "No tag found whose name ends with “" & "Dropped”"
return
end try
set theModifiedProjects to every flattened project -- where its modification date is greater than theStartDate and modification date is less than theEndDate
-- Loop through any detected projects
repeat with a from 1 to length of theModifiedProjects
set theCurrentProject to item a of theModifiedProjects
-- Retrieve any project tasks modified within the specified scope
set theCompletedTasks to (every flattened task of theCurrentProject where its completed = true and modification date is greater than theStartDate and modification date is less than theEndDate and number of tasks = 0)
-- Loop through any detected tasks
if theCompletedTasks is not equal to {} then
set modifiedTasksDetected to true
-- Append the project name to the task list
set theProgressDetail to theProgressDetail & "<h2>" & name of theCurrentProject & "</h2>" & return & "<br><ul>"
repeat with b from 1 to length of theCompletedTasks
set theCurrentTask to item b of theCompletedTasks
if primary tag of theCurrentTask is equal to droppedTag then
set theDroppedTasks to theDroppedTasks & "<li>" & name of theCurrentTask & " from Project: " & name of theCurrentProject & "</li>" & return
end if
-- Append the tasks's name to the task list
set theProgressDetail to theProgressDetail & "<li>" & name of theCurrentTask & "</li>" & return
end repeat
set theProgressDetail to theProgressDetail & "</ul>" & return
end if
end repeat
end tell
end tell
if theDroppedTasks is not equal to "" then
set theProgressDetail to theProgressDetail & "<h2>Dropped Tasks</h2>" & return & "<ul>" & return & theDroppedTasks & return & "</ul>" & return
end if
set theProgressDetail to theProgressDetail & "</body></html>"
-- Notify the user if no projects or tasks were found
if modifiedTasksDetected = false then
display alert "OmniFocus Completed Task Report" message "No modified tasks were found for " & theReportScope & "."
return
end if
-- Prepare a name for the new note
set {year:y, month:m, day:d} to theEndDate
set yearText to y as string
set result to y * 10000 + m * 100 + d as string
set theNoteName to result & " Task Completion Report"
-- Create the note in Evernote.
tell application "Evernote"
activate
set theNote to create note notebook "OF Task Completion Reports" title theNoteName with html theProgressDetail tags {yearText, "lists", "report"}
open note window with theNote
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment