Automatically Create an OmniFocus Task with AppleScript and BASH

Thanks to the Mac Power Users podcast, I discovered OmniFocus about a year and a half ago and it changed my lifeSeriously. I could (and very well may) write an entire post about my love for OmniFocus, but today I want to talk about mental friction and how I elevated a little of mine. Tools like OmniFocus are great for drastically reducing stress by allowing you to easily get all the stuff in your head into a trusted system, process it and take action on it as needed. If you’re anything like me though you put everything into your system and end up some tasks that, in some cases, repeat daily wether they’re actionable or not. The repeating task that prompted me to figure out how to automatically create an OmniFocus task was “Process action folder”. The concept of the action folder is simple: as things come into your system that need to be dealt with later in the day, they’re moved to the action folder and that folder is processed, well, later that day. I chose to put my action folder on Dropbox and having a universally accessible location where I could drop and continue with whatever I was doing while knowing it would get dealt with later was awesome! That is, until I discovered DevonTHINK.

DevonTHINK has entirely replaced Evernote for me and I’m extremely happy it did. I was never entirely happy with the way Evernote handled text formatting and not being able to hyperlink text in a note on iOS was (and still is) simply inexcusable, but I digress. I use DevonTHINK primarily as a personal wiki and much of the data that gets saved into Dropbox by Hazel or some other method also gets sent to DevonTHINK where I can file it accordingly. Yes, that means I have duplicate data in many cases, but I treat DevonTHINK as my working dataset while I think of Dropbox as archival and sharable storage. Having two distinct datasets really works well for me and gives me the choice about what I want to put where, but it’s also a bit of a problem. What happens if I drop everything directly into my DevonTHINK inbox one day because none of it requires action on my part later? Well, my action folder is empty, but OmniFocus doesn’t know that. So, I’m left with a task in OmniFocus that repeats daily regardless of wether anything is in my action folder or not. Sure, I could just check the task off at the end of the day and be done with it, but I’m a geek with decent scripting skills and fairly regular blocks of free time and I decided I wanted a better way…an automated way.

The basic premise of what I wanted to do was simple enough: list a directory and create a specific task in OmniFocus if it contains files or folder. Otherwise do nothing. This way, I’d only have a task to process my action folder if it actually had something in it. Creating the task only if the folder contained something could be achieved with a simple if/else statement in a BASH script, but actually creating the task proved more difficult. I knew the task would have to be created with AppleScript, but my lack of familiarity with AppleScript combined with other obligations put this project on the back burner for a while. I was browsing David Sparks’ Blog one day and happened upon a post titled “Create OmniFocus Tasks with AppleScript”. Huh, exactly what I needed. I modified the script in his Blog post and let it do the heavy lifting of creating the task in OmniFocus. I now had the ability to create the task and do so only if my action folder contained something, but I was still facing two problems:

  1. I can drop something into my action folder at any point during the day so I need the script to poll the folder for changes almost continuously.
  2. In their current iterations, neither the BASH wrapper script nor the AppleScript have a way of checking to see if there’s already a task called “Process action folder” in OmniFocus. So, once my action folder contained something, another instance of the task would be created each time the script ran until I processed the folder. Talk about motivation!

Thankfully, both problems had rather simple solutions:

  1. I use cron to run the BASH wrapper script every minute using the following syntax
  2. I use the concept of “drop files” to determine if the task is created, regardless of whether my action folder has something in it or not. Drop files are super simple in that they’re nothing more than a blank file with a date suffix (to make them unique) that gets created after the AppleScript runs and creates the task on a given day. Once the drop file for a given day exists, the BASH wrapper script won’t execute the AppleScript any more during that day. This solution doesn’t account for every possible edge case, but it generally works and prevents my OmniFocus list from being filled by the same task thousands of times.

After creating a file in my action folder such as the file shown in the following screenshot:

I’ll have the following task automatically appear in my OmniFocus list for the day:

That’s all it took to automatically create a task in OmniFocus task to process my action folder only if it actually contained something. Having one less thing on my task list every morning has definitely spared me some mental friction and allowed me a little more time to do things like write this post. The coolest thing, though, is that the principles discussed in this post can be adapted and used with other processes to do almost anything. For instance, you could use the principles (and some of the code) discussed here to monitor a shared folder and receive an email anytime new content is added.

Please see below for complete code samples of both the BASH wrapper script and the AppleScript and, as always, please don’t hesitate to leave a comment with any questions or comments.

BASH wrapper script

AppleScript

–J

Leave a Reply

Your email address will not be published. Required fields are marked *