Skip to content

Instantly share code, notes, and snippets.

@oreales
Last active December 4, 2015 00:56
Show Gist options
  • Save oreales/915bbc1d1f4bf6476539 to your computer and use it in GitHub Desktop.
Save oreales/915bbc1d1f4bf6476539 to your computer and use it in GitHub Desktop.
Applescript Omnifocus que uso para delegar tareas en mi flujo de GTD
tell application "OmniFocus"
try
set dialogReply to (display dialog ¬
"A quien delegas?" with title ¬
"Nombred del contacto a quien delegas..." default answer ¬
"Contacto en mi AddressBook" buttons {"Preparar Mail", "Delegar"} default button 2)
set delegateTo to text returned of dialogReply
set buttonReturned to button returned of dialogReply
if buttonReturned is "Preparar Mail" then
set dontSendEmail to true as boolean
else
set dontSendEmail to false as boolean
end if
tell contacts to set personEmail to getEmailOfContactForContext(delegateTo)
tell front window
set selectedTasks to selected trees of content
repeat with selectedTask in selectedTasks
set taskValue to get value of selectedTask
set taskId to get id of taskValue
set taskName to get name of taskValue
set taskNote to get note of taskValue
set taskProject to get containing project of taskValue
set taskContext to get context of taskValue
set taskContextName to name of taskContext
if class of taskProject is project then
set taskLabel to get name of taskProject
else
set taskLabel to ""
end if
set taskLabel to my replaceText(" ", "_", taskLabel)
set taskName to taskName & " @" & taskLabel
tell sendMail to set isDelegated to sendMailWithTask(taskName, taskNote, personEmail, dontSendEmail)
if dontSendEmail is true then
display alert taskName & " se ha preparado email pero no se ha enviado. Envialo manualmente y luego cambia el contexto a delegado."
else if isDelegated is true then
(* cambiariamos el contexto a delegado *)
(** vamos a coger el nombre de todos los contextos parents del contexto de esta tarea (containers) **)
set hasContainer to true
set currentContext to taskContext
set taskContextFullName to taskContextName
repeat until hasContainer is false
set currentContext to container of currentContext
set theContainerName to name of currentContext
if theContainerName is not "OmniFocus" then
set taskContextFullName to theContainerName & ":" & taskContextFullName
else
set hasContainer to false
end if
end repeat
(** cambiamos el contexto de la tarea a lo delegado **)
tell front document
set newContextName to my replaceText("Hablar con", "Delegado", taskContextFullName)
set context of taskValue to my FindContext(newContextName)
end tell
display alert taskName & " Se ha delegado. Tu tarea tiene ahora el contexto " & newContextName
end if
end repeat
end tell
on error error_message number error_number
display alert ("No se ha podido delegar!") ¬
message error_message ¬
& (" Error number ") & error_number & "."
end try
end tell
(** handle para encontrar el contexto con ruta completa entre los contextos de of **)
on FindContext(strContext)
tell application "OmniFocus"
tell front document
set oContextList to complete strContext as context maximum matches 1
if (oContextList is {}) then
else
return context id (id of item 1 of oContextList)
end if
end tell
end tell
end FindContext
(** handle para buscar y reemplazar texto generico dentro de un subject **)
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
script sendMail
property sender : "oreales@gmail.com"
on sendMailWithTask(theSubject, theContent, personEmail, dontSendEmail)
tell application "Mail"
set toName to item 1 of personEmail
set toRecipient to item 2 of personEmail
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:toName, address:toRecipient}
if dontSendEmail is false then
##Send the Message
set isSended to send theMessage
else
set isSended to false
end if
end tell
end tell
return isSended
end sendMailWithTask
end script
script contacts
on getEmailOfContactForContext(theContext)
tell application "Contacts"
set theContact to person theContext
set theName to name of theContact
set theEmails to emails of theContact whose label is "Work" or label is "omnifocus"
set theEmail to get value of first item of theEmails
end tell
return {theName, theEmail}
end getEmailOfContactForContext
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment