Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Maxim-Filimonov/1769115 to your computer and use it in GitHub Desktop.
Save Maxim-Filimonov/1769115 to your computer and use it in GitHub Desktop.
Copies selected or flagged tasks in Omnifocus to pomodoro app on ipad
-- Based on http://www.codenauts.com/blog/2011/3/1/importing-activities-from-omnifocus-or-things-to-pomodoro-ap.html and http://forums.omnigroup.com/showpost.php?p=55309&postcount=22
-- Gets selected tasks or flagged tasks and copies them to ~/Dropbox/Pomodoro/tasks.json
tell application "OmniFocus"
-- CONSTANTS change them if you like
set UserHome to (POSIX path of (path to home folder)) as string
set JSONfilename to UserHome & "Dropbox/Pomodoro/tasks.json" as string
set JSONpattern to UserHome & "Dropbox/Pomodoro/*.json" as string
set onePomodoroIsMinutes to 25
set estimateItems to {}
set UserHome to (POSIX path of (path to home folder)) as string
set JSONfilename to UserHome & "Dropbox/Pomodoro/tasks.json" as string
set JSONpattern to UserHome & "Dropbox/Pomodoro/*.json" as string
set estimateItems to {}
set JSONresult to ""
set JSONresults to {}
set JSONBeginning to "[
"
set JSONEnd to "]"
set JSONTaskBegin to " {
"
set JSONTaskEstimate to " \"estimate\":"
set JSONTaskEnd to " }
"
set JSONTaskListEnd to " },
"
set JSONTaskList to " \"list\":0,
"
set JSONTaskTitle to " \"title\":\""
set cmd to ""
set fileCheck to ""
set collectedTaskInfo to {}
-- "window 1" is usually the frontmost window
set theDoc to document window 1 of default document
-- collect all the highlighted tasks in that window
set TaskList to the value of the selected tree of the content of theDoc
if (count of TaskList) is 0 then -- no items are selected, so let's get all the flagged ones
-- the easiest way is to collect them from each context
repeat with currentContext in flattened contexts of default document
set TaskList to TaskList & (tasks of currentContext where blocked is false and flagged is true and completed is false)
end repeat
end if
repeat with theSelectedTask in TaskList
-- get some basic information from the task
set taskInfo to {TaskName:name, TaskNote:note, TaskStartDate:start date, TaskDueDate:due date, IsFlagged:flagged, TaskContext:name of context, TaskProject:name of containing project, TaskEstimate:estimated minutes} of theSelectedTask
set end of collectedTaskInfo to taskInfo
end repeat
end tell
repeat with taskInfo in collectedTaskInfo
if TaskEstimate of taskInfo is missing value then
set estimateDialog to display dialog "Estimate: " & (TaskName of taskInfo) default answer ¬
"2" buttons {"Cancel", "OK"} default button {"OK"}
if (button returned of estimateDialog is "OK") and (text returned of estimateDialog is not "") then
copy text returned of estimateDialog to the end of estimateItems
else if (button returned of estimateDialog is "OK") and (text returned of estimateDialog is "") then
set amount to "2" -- default estimate is 2
else if (button returned of estimateDialog is "Cancel") then
return
end if
else
set TaskEstimate to ((TaskEstimate of taskInfo) / onePomodoroIsMinutes) as integer
copy TaskEstimate to the end of estimateItems
end if
end repeat
set TasksCount to ((count (collectedTaskInfo)))
-- now we're stitching everything together
repeat with i from 1 to TasksCount
if i is not equal to TasksCount then -- last item needs to be handled differently. FU JSON!
set end of JSONresults to JSONTaskBegin & ¬
JSONTaskEstimate & (item i of estimateItems) & ",
" & ¬
JSONTaskList & ¬
JSONTaskTitle & (TaskName of item i of collectedTaskInfo) & "\"
" & ¬
JSONTaskListEnd
else
set end of JSONresults to JSONTaskBegin & ¬
JSONTaskEstimate & (item i of estimateItems) & ",
" & ¬
JSONTaskList & ¬
JSONTaskTitle & (TaskName of item i of collectedTaskInfo) & "\"
" & ¬
JSONTaskEnd
end if
end repeat
repeat with i from 1 to TasksCount
set JSONresult to JSONresult & (item i of JSONresults)
end repeat
set JSONresult to JSONBeginning & JSONresult & JSONEnd
-- check whether file exists
try
-- the file exists already. Let's delete it.
set fileCheck to do shell script "/bin/ls " & JSONfilename
if fileCheck is equal to JSONfilename then
set fileMove to "rm " & (quoted form of JSONpattern)
do shell script fileMove
end if
on error
-- apparently there's no tasks.json file yet, so let's create it
do shell script "touch " & (quoted form of JSONfilename)
end try
--return cmd
set cmd to "echo " & (quoted form of JSONresult) & " > " & (quoted form of JSONfilename) -- yeah, i know i suck for not using applescript here.
-- display dialog cmd
do shell script cmd
return JSONresult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment