Omnifocus delegation

After seeing Sven Fechner’s tweet about Sparrow’s scriptability and OmniFocus delegation and then looking over Don Southard’s code, I figured it would be easy enough to modify the script for use in Apple’s default Mail.app. As soon as I had finished up my modifications, I saw Benjamin Brooks post his modification of the code that did the exact same thing my code did. The exact same thing. I messaged him, he messaged me back and then I saw the modification he posted

… embeds an OmniFocus link to the script that will allow another OmniFocus user to add it with just a click … very cool!

For some reason I remember reading somewhere that the goals for OmniFocus versions (Ken Case, maybe. OmniFocus forums, maybe.) went something like 1.0 was for single users, 1.5 was syncing across devices and that 2.0 was for teams. I am super hyped for OmniFocus 2.0, but in the mean time, I have baked up another script.

My script relies on rides on the backs of several other Applescripts.

The script (I am sure this thing is horribly broken and awfully coded, but it does appear to work for me. Your mileage may vary, no warranties or guarantees. If this script breaks your stuff, please don’t hold me responsible, I have kids to feed and can’t afford being sued. CTRL-Z is your friend. Call your mother. Rights given up, no copyright attached. If I swiped your code, I mean no harm, see my note re: kids.) uses the above scripts and the is a modification of the Benjamin Brooks scripts. If the variables in the script are set correctly, the script will:

  1. Encode the OmniFocus task (for use in the email message)
  2. Run the Defer script (if indicated)
  3. Run the Delegate script (if indicated)
  4. Gin up the OmniFocus information for the email
  5. Set the message subject to user set text, plus task name
  6. Create the “Add to OmniFocus” URL (if indicated)
  7. Create the “Open in OmniFocus” URL (if indicated)
  8. Create the new email message with the OmniFocus information

Here is my thought process:

  • I agree with Ben that Contexts are individually defined.
  • As much as I want people to get back to me immediately, I don’t want them to expect me to sit on email, so I don’t expect them to. In most cases I can give them a couple of days to get back to me.
  • I like that the Delegate script automatically changes the context and prepends what ever I want to the task that is being delegated. My options are set to prepend “Delegated:” and change the context to “Waiting For”
  • I delegate tasks to my wife (a fellow OmniFocus user), so I want her to be able to add them quickly to her system.
  • I want to easy get back to the task when i do get a response, so I have the “Open In OmniFocus” link. This streamlines my task clean up when I do finally get a response. Please give it a whirl. Hit me on the Twitter (@jeredb) if it works (or not).

--by Don Southard aka @binaryghost adapted by Ben Brooks aka @benjaminbrooks, adapted by Jered Benoit aka @jeredb - 20113415

    -- Variables

    -- Email Subject Line 
    set mailSubject to "Delegated Task from Jered Benoit: "

    -- Include "Send to Omnifocus" link 
    set sendToOmnifocus to true

    -- Include "Open in Omnifocus" link 
    set openInOmniFocus to true

    -- Defer Applescript location 
    -- Available from: http://bylr.net/files/omnifocus/ 
    set runDeferScript to true 
    -- Has to be full path (no "~") 
    set DeferScript to "/Users/Jeredb/Library/Scripts/Applications/OmniFocus/Defer.scpt"

    -- Delegate Applescript location
    -- Available from: http://www.cerquant.com/software 
    set runDelegateScript to true 
    -- Has to be full path (no "~") 
    set DelegatedScript to "/Users/Jeredb/Library/Scripts/Applications/OmniFocus/Delegated.scpt"

    on urlencode(theText) 
        set theTextEnc to "" 
        repeat with eachChar in characters of theText 
            set useChar to eachChar 
            set eachCharNum to ASCII number of eachChar 
            if eachCharNum = 32 then 
                set useChar to "%20" 
            else if (eachCharNum ? 42) and (eachCharNum ? 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then 
                set firstDig to round (eachCharNum / 16) rounding down 
                set secondDig to eachCharNum mod 16 
                if firstDig > 9 then 
                    set aNum to firstDig + 55 
                    set firstDig to ASCII character aNum 
                end if 
                if secondDig > 9 then 
                    set aNum to secondDig + 55 
                    set secondDig to ASCII character aNum 
                end if 
                set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string 
                set useChar to numHex 
            end if 
        set theTextEnc to theTextEnc & useChar as string 
        end repeat 
    return theTextEnc end urlencode

    if runDeferScript is true then run script (DeferScript) 

    if runDelegateScript is true then run script (DelegatedScript)

    tell application "OmniFocus" 
        tell front document 
            tell (first document window whose index is 1) 
                set SelectedItemInMainView to selected trees of content 
                try 
                    set theSelectedTask to value of item 1 of SelectedItemInMainView
                    set nameSelectedTask to name of theSelectedTask
                    set noteSelectedTask to note of theSelectedTask
                    set idSelectedTask to id of theSelectedTask
                    -- set contextSelectedTask to name of the context of theSelectedTask
                    set encodedName to my urlencode(nameSelectedTask as rich text)
                    set encodedNote to my urlencode(noteSelectedTask as rich text)
                    set addToOFLink to "<omnifocus:///add?name=" & encodedName & "&note=" & encodedNote & ">"
                    set openInOFLink to "<omnifocus:///task/" & idSelectedTask & ">"

                    set mailSubject to mailSubject & nameSelectedTask
                    set mailContent to nameSelectedTask & return & "Note: " & noteSelectedTask & return
                    if sendToOmnifocus is true then set mailContent to mailContent & "Add this task to Omnifocus: " & addToOFLink
                    if openInOmniFocus is true then set mailContent to mailContent & return & "Open in Omnifocus: " & openInOFLink
                on error err
                    display dialog "No task selected!" with icon caution
                end try
                tell application "Mail"
                    set newMessage to make new outgoing message with properties {subject:mailSubject, content:mailContent}
                    tell newMessage
                        -- Default is false. Determines whether the compose window will
                        -- show on the screen or whether it will happen in the background.
                        set visible to true
                        tell content
                    end tell
                end tell
                -- Bring the new compose window to the foreground, in all its glory
                activate
            end tell
        end tell
    end tell
    end tell

Comments are closed.