Skip to content

Instantly share code, notes, and snippets.

@samuelkordik
Last active May 25, 2017 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelkordik/06cfc24bec357e9c413dda7933e3444e to your computer and use it in GitHub Desktop.
Save samuelkordik/06cfc24bec357e9c413dda7933e3444e to your computer and use it in GitHub Desktop.
CompleteTask
var appC = Application.currentApplication();
appC.includeStandardAdditions = true;
function fetchCurrentTask() {
var output = JSON.parse(appC.doShellScript("curl -v -u xxxx:api_token 'https://www.toggl.com/api/v8/time_entries/current'"));
if (output.data == null) {
return null;
} else {
return output.data;
}
}
function completeTogglTask(togglID) {
var URL = "https://www.toggl.com/api/v8/time_entries/"+togglID+"/stop";
var output = JSON.parse(appC.doShellScript('curl -v -u xxxx:api_token -X PUT "'+URL+'"'));
if (output.data == null) {
return null;
} else {
return output.data;
}
}
function findOFTask(togglID, dur) {
var app = Application("Omnifocus");
app.includeStandardAdditions = true;
var taskSet = app.defaultDocument.flattenedTasks.whose({note: {_contains: "Toggl ID: " + togglID}});
if (taskSet.length == 0) {
appC.displayAlert("No matching Omnifocus task found");
} else {
if (taskSet.length > 1) {
appC.displayAlert("Multiple matching Omnifocus tasks found");
}
var task = taskSet[0];
if (task.estimatedMinutes() != null) {
//appC.displayDialog("Overwrite existing time?") // update this to include better time details
var oldNote = task.note();
task.note = oldNote + '\nOriginal estimate: '+task.estimatedMinutes()+' min';
};
task.estimatedMinutes(dur);
task.completed = true;
return true;
}
}
var curTask = fetchCurrentTask();
if (curTask == null) {
appC.displayAlert('No task running.');
} else {
// findOFTask(curTask.id);
var d = new Date();
var seconds = Math.round(d.getTime()/1000);
var duration = Math.round( (curTask.duration + seconds) / 60 );
if (findOFTask(curTask.id, duration)) {
completeTogglTask(curTask.id);
}
}
tell application "OmniFocus"
tell front document
set togglID to do shell script "PATH=$PATH:/usr/local/bin; curl -u xxxx:api_token 'https://www.toggl.com/api/v8/time_entries/current' | jq '.data.id'"
set dur to do shell script "PATH=$PATH:/usr/local/bin; curl -u xxxx:api_token 'https://www.toggl.com/api/v8/time_entries/current' | jq '.data.duration'"
set epo to do shell script "date +'%s'"
set duration to (dur + epo) / 60 as text
set togglTag to "Toggl ID: " & togglID
set taskSet to (every flattened task whose note contains togglTag)
set theTask to first item in taskSet
tell theTask
if estimated minutes is not missing value then
set oldNote to note of theTask
set note of theTask to oldNote & "
Original estimate: " & (estimated minutes of theTask) & " min"
end if
set estimated minutes to duration
set completed to true
end tell
set theURL to "https://www.toggl.com/api/v8/time_entries/" & togglID & "/stop"
do shell script "curl -v -u xxxx:api_token -X PUT " & theURL
end tell
end tell
tell application "OmniFocus"
tell front document
tell content of document window 1
set theSelectedItem to value of first selected tree
set itemTitle to name of theSelectedItem
set ofID to "of_" & id of theSelectedItem
tell application "JSON Helper"
set payload to make JSON from {time_entry:{description:itemTitle, tags:{"omnifocus", ofID}, created_with:"curl"}} without pretty printing
set output to do shell script "curl -v -u xxxx:api_token -H 'Content-Type: application/json' -d '" & payload & "' https://www.toggl.com/api/v8/time_entries/start"
set Pos to offset of "id\":" in output
set workStr to get characters (Pos + 4) thru end of output as string
set Pos to offset of "," in workStr
set theID to get characters 1 thru (Pos - 1) of workStr as string
end tell
set theNote to the note of theSelectedItem
set note of theSelectedItem to theNote & "
Toggl ID: " & theID
end tell
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment