Write Today’s Completed Tasks In OmniFocus to a Text File

Description

This AppleScript uses a handler that RobTrew shared on the OmniFocus Forum to get today's completed tasks and write them to a text file. (Note: Requires OmniFocus 1.8+) For people who are just learning how to use AppleScript handlers, I've added the necessary code needed to save the list to a file and annotated the script. Think it's a great example of how these modular bits of code ('handlers', 'subroutines, etc.) can be used -- and re-used -- in any number of your AppleScript projects!

The Code

(*
Veritrope.com
Write Today's Completed Tasks In OmniFocus to a Text File
Version 1.0
April 30, 2011

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/write-todays-completed-tasks-in-omnifocus-to-a-text-file

// SCRIPT BASED UPON ROBTREW'S HANDLER
--ORIGINALLY POSTED ON THE OMNIFOCUS FORUM:
http://forums.omnigroup.com/showthread.php?t=13849


// RECOMMENDED INSTALLATION INSTRUCTIONS:

FastScripts Installation (Optional, but recommended):
--Download and Install FastScripts from http://www.red-sweater.com/fastscripts/index.html
--Copy script or an Alias to ~/Library/Scripts/Applications/OmniFocus
--Set up your keyboard shortcut
*)



(*
======================================
// MAIN PROGRAM
======================================
*)


--GET TODAY'S DATE
set dteToday to date (short date string of (current date))

--CHOOSE FILE NAME / SAVE LOCATION
set theFilePath to choose file name default name "Today_Task.txt"

--GET OMNIFOCUS INFORMATION
set today_Tasks to my do_OmniFocus()

--WRITE THE TEXT FILE
my write_File(theFilePath, today_Tasks)

(*
======================================
// MAIN HANDLER SUBROUTINES
======================================
*)


--GET OMNIFOCUS INFORMATION
on do_OmniFocus()
    set dteToday to date (short date string of (current date))
    tell application id "com.omnigroup.OmniFocus"
        tell default document
            set refDoneToday to a reference to (flattened tasks where (completion date ≥ dteToday))
            set {lstName, lstContext, lstProject} to {name, name of its context, name of its containing project} of refDoneToday
            set strText to ""
            repeat with iTask from 1 to count of lstName
                set {strName, varContext, varProject} to {item iTask of lstName, item iTask of lstContext, item iTask of lstProject}
                set strText to strText & "OmniFocus: " & tab & strName
                if varContext is not missing value then set strText to strText & " @" & varContext
                if varProject is not missing value then set strText to strText & " (" & varProject & ")"
                set strText to strText & return
            end repeat
        end tell
    end tell
    strText
end do_OmniFocus


(*
======================================
// UTILITY SUBROUTINES
======================================
*)


--EXPORT TO TXT FILE
on write_File(theFilePath, today_Tasks)
    set theText to today_Tasks
    set theFileReference to open for access theFilePath with write permission
    write theText to theFileReference as «class utf8»
    close access theFileReference
end write_File

6 Responses to “Write Today’s Completed Tasks In OmniFocus to a Text File”

  1. Joe S April 29, 2011 at 11:39 pm #

    Where does it put the text file?

    • Justin Lancy April 30, 2011 at 12:40 pm #

      Hi Joe,

      I’ve updated the code to be a full AppleScript — not just a “handler”. The new version should ask you where you want to save the text file.

      Hope you enjoy!

  2. Joe S April 30, 2011 at 4:47 pm #

    Thanks so much. I’m very familiar with programming, but completely new to Applescript and since its so different than other lang’s I write in, I wasn’t sure where to start. Thanks so much. If I make mods, I’ll share them with you.

    • Justin Lancy April 30, 2011 at 5:28 pm #

      My pleasure!

      A big part of why I’ve set up a “Code Library” is for this very purpose — To demystify AppleScript and to get people started on projects that they can share with other Mac Users! I’m so glad you “raised your hand” to let me know that the old write-up wasn’t clear… probably helped a number of people by doing so! 😉

      Anyhow, have fun tinkering — and be sure to share your scripts back with the group. I think that the process of learning to program is just like learning how to write well — If you read good stuff by others, the better your own stuff will be over time. (Hopefully!)

  3. Elliot Betancourt February 24, 2012 at 11:32 am #

    Just a note: If you are using the Mac App Store version of OmniFocus, you need to change the application id in the second line of the do_OmniFocus routine to be:

    “com.omnigroup.OmniFocus.MacAppStore”

    Works perfectly then!

    thanks!

    • Justin Lancy February 24, 2012 at 1:35 pm #

      A great tip, Elliot — Thanks!