Skip to content

Instantly share code, notes, and snippets.

@doug4j
Created January 12, 2019 14:40
Show Gist options
  • Save doug4j/8b3e7b7f8750c6e2003c482cbde5bbef to your computer and use it in GitHub Desktop.
Save doug4j/8b3e7b7f8750c6e2003c482cbde5bbef to your computer and use it in GitHub Desktop.
//Also see question and answer at https://discourse.omnigroup.com/t/add-a-new-task-and-set-the-project-it-belongs-to-via-javascript-of3/44111
var app = Application('OmniFocus');
app.includeStandardAdditions = true;
var doc = app.defaultDocument;
var content = doc.documentWindows.at(0).content;
var selectedTree = content.selectedTrees();
var selectedLen = selectedTree.length
var proj = null
if (selectedLen == 0) {
app.displayDialog("Nothing selected in Omnifocus");
} else if (selectedLen > 1) {
app.displayDialog("More than one object selected in Omnifocus");
} else {
try{
var proj = selectedTree[0]
proj.value().status()
}catch(e) {
proj = null
app.displayDialog("Selected item is not a Project");
}
}
if (proj !== null) {
app.displayDialog("Project Selected"
+ "\nid: [" + proj.id() + "]\nname: [" + proj.name()
+ "]\nstatus: " + proj.value().status() + "\nnumberOfAvailableTasks: [" + proj.value().numberOfAvailableTasks() + "]");
var newTask = app.Task({
name: 'sample task',
note: 'a note',
});
//Bingo... this is how you push a task into a project.
proj.value().tasks.push(newTask);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment