Using Keyboard Maestro to Create an Always Up to Date Task Calendar from My OmniFocus

Sep 25

OmniFocus 2 had a calendar you could subscribe to, which gave you a calendar view of your tasks. Unfortunately just because of internet connections and syncing times which was always a little flakey (completed tasks would hang around for a while) and didn't have all of the options I wanted. Unfortunately very few people used this service so it was discontinued in OmniFocus 3 - but I have an alternative!

Someone in the OmniFocus Slack pointed me to this post in the OmniFocus forums to help you create calendar events from your OF database and I got stuck in.

The first thing I decided was that OmniFocus should have its own calendar, this made life much easier later so I highly recommend you do this too. The next step was to decide how long events should be input for if they have no estimated time. The old built in option used an hour which I found too long so I set mine to 30 minutes.

I am using Keyboard Maestro to run this script hourly, but to prevent duplicate events showing up it makes sense to delete the existing events first. There's no finesse here, if it's in this calendar it gets deleted - this is where having a dedicated calendar makes life easier, you could write AppleScript to just delete specific calendar events, or even to track which tasks in OmniFocus have been created or edited since the script last ran - but I was aiming for the simplest and fastest solution here.

On to the code itself

property calendar_name : "OmniFocus" -- This is the name of your calendar
property default_duration : 30 --minutes

-- Rosemary Orchard
-- Modified from a script by unlocked2412
-- This creates calendar events for tasks which have a due date, if an estimated time is not set then the task defaults to 30 minutes in length

tell application "Calendar"
    set calendar_element to calendar calendar_name
    tell calendar calendar_name
        set theEvents to every event
        repeat with current_event in theEvents
            delete current_event
        end repeat
    end tell
end tell

tell application "OmniFocus"
    tell default document
        set task_elements to flattened tasks whose ¬
            (completed is false) and (due date ≠ missing value)
        repeat with item_ref in task_elements
            -- GET OMNIFOCUS TASKS
            set the_task to contents of item_ref
            set task_name to name of the_task
            set task_note to note of the_task
            set task_due to due date of the_task
            set task_estimate to estimated minutes of the_task
            set task_url to "omnifocus:///task/" & id of the_task
            if task_estimate is missing value then set task_estimate to default_duration
            -- BUILD CALENDAR DATE
            set end_date to task_due
            set start_date to start_date - (task_estimate * minutes)
            -- CREATE CALENDAR EVENT
            tell application "Calendar"
                tell calendar_element
                    if not (exists (first event whose (start date = start_date) and (summary = task_name))) then
                        make new event with properties ¬
                            {summary:task_name, start date:start_date, end date:end_date, url:task_url} at calendar_element
                    end if
                end tell
            end tell
        end repeat
    end tell
end tell

To summarise: Every incomplete task with a due date is added to the calendar.

  • The name of the task becomes the name of the event.
  • The task note becomes the event note
  • If the task has an estimated duration this is used, otherwise the event is set to whatever the default_duration property is at the top of the list (30 minutes in this example)
  • The due date and the duration of the event are used to calculate the start date
  • The URL of the event is set to be a URL back to the task

Graphical view of the Keyboard Maestro Macro

I don't use the calendar much myself (preferring Fantastical) so I also have Keyboard Maestro quit the calendar afterwards for me.

This Macro uses the cron timer in Keyboard Maestro to run hourly, I've added it to a specific group so it only runs on one of my Macs too. When you download it you will need to enable it, first make sure you create an OmniFocus calendar though, and if you give this calendar a different name then you'll need to update the Macro too.

This approach has several advantages over the original OmniFocus implementation:

  • I can sync this calendar, and store it in whichever service I like.
  • I can control the default length of events.
  • I could choose to exclude certain events if I wished to.
  • It could be a jumping off point for other scripts - such as a calendar of events you have completed if you wanted one of those.

You could of course use other approaches to run the script automatically, or run it manually when you want to, it's entirely up to you!

The script can be downloaded here.

keyboard maestro OmniFocus

Related Posts

Webmentions

Comments

Mark
Made change as per David's comment but still got error. Worked after I changed it from: set start_date to due_date - (task_estimate * minutes) to set start_date to task_due - (task_estimate * minutes)
Fri, 19 Oct 2018 14:39:42
David
Change the respective line in the script as follows, then the start_date not defined error is resolved. Didn’t get the task note carried across to event note though. >>> set start_date to due_date - (task_estimate * minutes)
Mon, 01 Oct 2018 23:45:07
Gerrit
liked this
Mon, 01 Oct 2018 12:42:10
David
I receive the same error saying start_date is not defined....
Sun, 30 Sep 2018 17:11:13
Andrew
When I try running this I get a script error saying start_date is not defined. Any idea why this would be happening?
Thu, 27 Sep 2018 11:25:22

Add a comment

Previous Post Next Post