Skip to content

Instantly share code, notes, and snippets.

@traviskirton
Last active April 24, 2017 17:58
Show Gist options
  • Save traviskirton/8a93c6abcd8ab91a43cda71e5f73eab1 to your computer and use it in GitHub Desktop.
Save traviskirton/8a93c6abcd8ab91a43cda71e5f73eab1 to your computer and use it in GitHub Desktop.
OmniFocus send tasks script
# Copyright 2017 Travis Kirton
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# I learned a lot from [Jered Benoit's](http://jeredb.com) [Delegate and Defer.scpt](https://github.com/jeredb/Delegated-and-deferred)
property personContext : ""
property people : {"john"} #add more names here, and make sure they line up with the emails
property emails : {"john@somewhere.com"} #add an email for every name in the previous variable
property emailTo : ""
on main()
try
tell application "OmniFocus"
set selectedTasks to my grabSelectedTasks()
if ((count of selectedTasks) < 1) then
my alert("You need to select at least 1 task.")
else
set dialogAnswer to my personToReceiveTasks()
set personContext to (the text returned of dialogAnswer) as string
set handleCreation to (the button returned of dialogAnswer) as string
if handleCreation = "Send it!" then
my confirmDatesFor(selectedTasks)
my createEmail(selectedTasks)
my createFollowUps(selectedTasks)
end if
end if
end tell
my sync()
end try
end main
on confirmDatesFor(selectedTasks)
tell application "OmniFocus"
repeat with selectedItem in selectedTasks
set currentTask to value of selectedItem
if due date of the currentTask is missing value then
set due date of the currentTask to my createDueDate()
end if
if defer date of the currentTask is missing value then
set defer date of the currentTask to my createDeferDate(due date of the currentTask)
end if
end repeat
end tell
end confirmDatesFor
on personToReceiveTasks()
display dialog "To whom should I send the task(s)?" default answer "john" buttons {"Nahhhh", "Send it!"} default button 2
end personToReceiveTasks
on grabSelectedTasks()
tell application "OmniFocus"
tell first document
tell (first document window whose index is 1)
return selected trees of content
end tell
end tell
end tell
end grabSelectedTasks
on createEmail(selectedTasks)
set mailContent to ""
tell application "OmniFocus"
repeat with selectedItem in selectedTasks
set newTask to value of selectedItem
set newLink to my createLink(newTask)
set mailContent to mailContent & newLink & return
end repeat
end tell
set emailAddress to item (getPositionOfItemInList((personContext as string), people)) of emails
tell application "Mail"
set newMail to make new outgoing message with properties {subject:"New Tasks", visible:true}
tell newMail
set the sender to "Travis <travis@c4studio.co>"
set content to mailContent
set visible to true
make new to recipient with properties {name:personContext, address:emailAddress}
end tell
activate
end tell
end createEmail
on getPositionOfItemInList(theItem, theList)
repeat with a from 1 to count of theList
if item a of theList is theItem then return a
end repeat
return 0
end getPositionOfItemInList
on createFollowUps(selectedTasks)
tell application "OmniFocus"
repeat with selectedItem in selectedTasks
set currentTask to value of selectedItem
if the containing project of the currentTask is not missing value then
tell containing project of currentTask
set followUp to make task with properties {{name:(name of currentTask), note:("Delegated on: " & (current date) as string) & return & return & (note of the currentTask) as string, context:my createContext(personContext)}}
set due date of followUp to the due date of currentTask
set defer date of followUp to the defer date of currentTask
set completed of currentTask to true
end tell
else
display dialog "not ok"
end if
end repeat
end tell
end createFollowUps
on createContext(contextName)
tell application "OmniFocus"
tell first document
try
set contextID to id of item 1 of (complete contextName as context)
set theContext to first flattened context whose id is contextID
end try
end tell
end tell
return theContext
end createContext
on createLink(currentTask)
tell application "OmniFocus"
if name of the currentTask is not missing value then
set currentLink to "omnifocus:///add?name=" & my encode(name of currentTask)
if note of the currentTask is not missing value then
set theNote to text of (note of the currentTask)
if theNote is not equal to "" then
set encodedNote to my encode(theNote)
set currentLink to currentLink & "&note=" & encodedNote
end if
end if
if due date of currentTask is not missing value then
set encodedDate to my encode(due date of currentTask as string)
set currentLink to currentLink & "&due=" & encodedDate
end if
if defer date of currentTask is not missing value then
set encodedDefer to my encode(defer date of currentTask as string)
set currentLink to currentLink & "&defer=" & encodedDefer
end if
if flagged of the currentTask is true then
set currentLink to currentLink & "&flagged=true"
end if
if the containing project of the currentTask is not missing value then
set projectName to (name of the containing project of the currentTask) as string
set currentLink to currentLink & "&project=" & projectName
end if
return name of the currentTask & ":" & return & currentLink & return & return
else
say "task has no name"
end if
end tell
end createLink
on createDueDate()
set newDate to ((current date) + (86400 * 7))
return newDate
end createDueDate
on createDeferDate(dueDate)
set dateOffset to (dueDate - (current date)) / 2
set deferDate to ((current date) + dateOffset)
return deferDate
end createDeferDate
on alert(statement)
tell application "OmniFocus"
tell default document
if statement is not "" then display alert statement as warning
error number -128
end tell
end tell
end alert
on encode(theText)
return replace_chars(theText, " ", "%20")
end encode
#via: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
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"
end if
set theTextEnc to theTextEnc & (useChar as string)
end repeat
log theTextEnc
return theTextEnc
end urlencode
on sync()
try
synchronize
end try
end sync
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment