Update task in OmniFocus with Applescript

Hi, I’m really new to Applescript, so I might be missing something obvious, but I’m trying to figure out how to update the note on an existing task, and even automatically mark a task as resolved. Does anyone have an example of this?

set theDate to current date
set theTask to "My Task"
set theNote to "My First Description"

tell application "OmniFocus"
    tell front document
        set theContext to first flattened context where its name = "My Context"
        set theProject to first flattened project where its name = "My Project"
        tell theProject to make new task with properties {{name:theTask, note:theNote, context:theContext}}
    end tell
end tell

I use JIRA for my coding issues, and I’ve got a python app which gets my latest issues from JIRA and checks to see if they exist in OmniFocus using AppleScript, and then adds them if they’re new… but then I’m tracking the status in JIRA, and would love to find a way to feed the status back to my issues in OmniFocus!

Okay, figured it out. Didn’t realize I could open the Script Editor and open File -> Open Dictionary to see the OmniFocus structure.

Here’s what I’ve ended up using:

on run
    set theDate to current date
    set theTask to "My Task"
    set theStatus to "Status: In Progress\n"
    set theFlag to true
    set theDueDate to date "10/12/2014"
    set theNote to "Status: In Progress\nMy detail for the project."
    set theContextName to "My Context"
    set theProjectName to "My Project"

    tell application "OmniFocus"
        tell front document
            set theContext to first flattened context where its name = theContextName
            set theProject to first flattened project where its name = theProjectName

            if exists (first flattened task where its name = theTask and its context = theContext) then
                -- Tasks exists, update the detail if necessary
                set selectedTask to first flattened task where its name = theTask
                set selectedNote to the paragraphs of the note of the selectedTask
                -- Overwrite the note detail if the status has changed
                if (item 1 of the selectedNote is not theStatus) then
                    set the note of the selectedTask to theNote
                end if
                -- Flag task if necessary (but don't remove existing flags)
                if (not flagged of the selectedTask and theFlag) then
                    set the flagged of the selectedTask to theFlag
                end if
                -- Check due date
                set selectedDueDate to the the due date of the selectedTask
                if (theDueDate is not missing value and theDueDate is not selectedDueDate) then
                    set the due date of the selectedTask to theDueDate
                end if
                -- Ensure task not marked as completed
                if (completed of the selectedTask) then
                    set the completed of the selectedTask to false
                end if
            else
                -- Add new Task
                tell theProject to make new task with properties {{name:theTask, note:theNote, context:theContext}}
            end if
        end tell
    end tell
end run
1 Like

Hey -

I’m in the middle of writing something very similar for my work. If you felt like sharing your code (python and/or applescript), I’d appreciate it. I’m trying to write mine with the appscript module in python. Sometimes the “guessing” of how real apple script translates to appscript is a pain, but I’m getting there.

-Tom

Sure thing - added a gist with the full code here –