Skip to content

Instantly share code, notes, and snippets.

@doug4j
Last active October 26, 2021 17:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doug4j/45c1745b4fc65f3e41c1da92dcfe087c to your computer and use it in GitHub Desktop.
Save doug4j/45c1745b4fc65f3e41c1da92dcfe087c to your computer and use it in GitHub Desktop.
// jshint esversion:6
(function() {
var app = Application('OmniFocus'); //This assumes that OmniFocus is installed on MacOS
app.includeStandardAdditions = true;
var doc = app.defaultDocument;
var content = doc.documentWindows.at(0).content;
var selectedTree = content.selectedTrees();
var selectedLen = selectedTree.length;
var totalItems = 0;
var totalMinutes = 0;
for (var i=0;i < selectedLen; i++) {
try{
var task = selectedTree[i];
//var hasParentTask = false;
if (isLeafTask(task)) {
var estimatedMinutes = task.value().estimatedMinutes();
if (estimatedMinutes !== null) {
totalMinutes = totalMinutes + estimatedMinutes;
}
totalItems = totalItems + 1;
}
}catch(e) {
if (e.message === "User canceled.") {
return; //get out of the loop (end the program)
} else {
app.displayDialog("[Exception] Content Selected index [" + i + "] is not a task: " + e);
return;
}
}
}
function isLeafTask(item) {
try {
if(item.value().inInbox()) {
return true;
}
if (item.value().containingProject().id() === item.value().id()) {
//This is a project, tag, or some other top-level container
return false;
}
return item.value().numberOfTasks() == 0;
} catch (e) {
return false;
}
}
var msg = "";
if (totalItems > 1) {
msg = "" + Math.floor(totalMinutes / 60) + "h " + (totalMinutes % 60).toFixed(0) + "m " + totalItems + " items";
} else {
msg = "" + Math.floor(totalMinutes / 60) + "h " + (totalMinutes % 60).toFixed(0) + "m " + totalItems + " item";
}
app.displayDialog(msg);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment