Add a new task and set the project it belongs to via javascript (OF3)

I’m trying to add a new task and set the project it belongs. I prefer to use javascript. Yet, nothing I do seems to work. The below allows the user to select the given project and the thought is that having the project selected this project will be set to a new hard coded new task. I tried setting the “assignedContainer” (an undocumented feature?) per a post. I also tried assigning the task a different container. And, I also tried putting the document in the inbox and then moving it. Nothing seems to work. I am only able to get the task to the inbox but it has no project assigned to it.

I’m using Omnifocus 3. Thanks for any help. Here’s my sample:

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',
	//from https://discourse-test.omnigroup.com/t/problems-assigning-a-project-to-a-task-with-hazel/21972
	//I thought I needed to set the containe. This does not fail, but it does not put it in the correct project.
	//The only way I seem to be able to add the task is to put it in the inbox... where is not where I want it to go.
	assignedContainer: proj, 
  });
  app.quickEntry.inboxTasks.push(newTask);
  app.quickEntry.save(); //Non-Interactive
  
  
  //Tried using the 'move' method from here https://discourse-test.omnigroup.com/t/set-the-project-for-a-task/11588
  //but it seems this is only available in Objective-C?
  //app.move( newTask, { to: proj.value().id }); //The move command requests a list of ...
  //newTask.move( { to: proj.value().id }); //The move command requests a list of ...
  //newTask.move(proj.value().id()); //Named Parameters must be passed as an object
  
  //Can't push this task into any other container
  //doc.tasks.push(newTask); //Apple Event Handler Failed
  //app.add({ add: newTask, to: proj.value().id()}); //Can't Convert types
  //proj.defaultDocument.quickEntry.push(newTask) //Can't Convert types
  //proj.quickEntryTrees().inboxTasks.push(newTask) //Can't convert types
}

Yeah, you don’t want to add it to the inbox. Just add it directly to the project you want to save it in.

Instead of pushing to app.quickEntry.inboxTasks, push newTask onto proj.tasks.

Good luck!

1 Like

Much appreciated colter. I was able to get it going by your tip.

Here is the full working example for anyone downstream:

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);
}