Creating a new task with javascript for automation (JXA)

I am trying to create a script which will create a new task with specific properties using JXA.

So far I have the following which works:

of = Application('OmniFocus');
somedate = Date( 2016, 6, 1);
inbox = of.defaultDocument.inboxTasks;
task = of.InboxTask({ 
	name: 'sample task',
	note: 'a note', 
	deferDate: somedate
});
inbox.push(task);

However, I can’t set the context, e.g.

task = of.InboxTask({ 
	name: 'sample task',
	note: 'a note',
	context: 'work',
	deferDate: somedate
});

This gives me Error: Can't convert types. - presumably context has to be passed as an object rather than a string?

Ideally, I would like to pop up the quick entry box with name, note, context etc set as defaults, but allowing me the option to change options before creating the task.

Is it also possible to use JXA to bring up a pre-filled quick entry box?

In order to use the Quick Entry box you have to adress the “quickEntry” object.
You can set the context of the newly created task but you have to pass the actual context object, not just the name.

As a starting point, this snippet might help:

var of = Application('OmniFocus'),
ofDoc = of.defaultDocument,
inbox = ofDoc.inboxTasks,
myContext = ofDoc.flattenedContexts.whose({name: 'work'})[0];

task = of.InboxTask({ 	name: 'sample task',note: 'a note', context: myContext});
of.quickEntry.inboxTasks.push(task);
of.quickEntry.open();
1 Like

That is perfect - thank you! For the benefit of anyone trying to do something close to what I’m doing - namely adding a Mailplane email to OmniFocus (with more control than the default option provided by Mailplane) - here’s the full script:

mp = Application('Mailplane 3');
url = mp.currenturl();
title = mp.currenttitle();

of = Application('OmniFocus');
of_doc = of.defaultDocument;
of_qe = of.quickEntry;

//get email context
email_context = of_doc.flattenedContexts.whose({name: 'email'})[0];

//default defer to 9am tomorrow
defer = new Date();
defer.setDate( defer.getDate() +1 );
defer.setHours( 9 );
defer.setMinutes( 0 );
defer.setSeconds( 0 );

task = of.InboxTask({ 
	name: 'email: ' + title,
	note: url + "\n",
	context: email_context,
	deferDate: defer
});

//create task in quick entry box
of_qe.inboxTasks.push(task);
of_qe.open();
2 Likes