Skip to content

Instantly share code, notes, and snippets.

@cescoffier
Last active September 5, 2016 07:11
Show Gist options
  • Save cescoffier/36e8331b57b5ff89ecd38ae17e3934ac to your computer and use it in GitHub Desktop.
Save cescoffier/36e8331b57b5ff89ecd38ae17e3934ac to your computer and use it in GitHub Desktop.
ObjC.import('Cocoa');
ObjC.import('Foundation')
var args = $.NSProcessInfo.processInfo.arguments
var argv = []
var argc = args.count // -[NSArray count]
for (var i = 0; i < argc; i++) {
argv.push( ObjC.unwrap( args.objectAtIndex(i) ) ) // -[NSArray objectAtIndex:]
}
delete args
var done = [];
var app = Application("OmniFocus");
var doc = app.defaultDocument;
var tasks = doc.flattenedTasks.whose({id: argv[2]});
var completedTasks = [];
for (var i = 0; i < tasks.length; i++) {
tasks[i].completed = true;
completedTasks.push(tasks[i].name());
}
completedTasks;
ObjC.import('Cocoa');
ObjC.import('Foundation')
var args = $.NSProcessInfo.processInfo.arguments
var argv = []
var argc = args.count // -[NSArray count]
for (var i = 0; i < argc; i++) {
argv.push( ObjC.unwrap( args.objectAtIndex(i) ) ) // -[NSArray objectAtIndex:]
}
delete args
var done = [];
var app = Application("OmniFocus");
var doc = app.defaultDocument;
var tasks = doc.flattenedTasks.whose({id: argv[2]});
var contexts = doc.flattenedContexts;
var waiting;
for (var i = 0; i < contexts.length; i++) {
if (contexts[i].name() == "Waiting") {
waiting = contexts[i];
}
}
if (! waiting) {
throw "Unable to find the 'Waiting' context"
}
var movedTasks = [];
for (var i = 0; i < tasks.length; i++) {
tasks[i].context = waiting;
tasks[i].note = argv[3];
movedTasks.push(tasks[i].name());
}
movedTasks;
var report = [];
var app = Application("OmniFocus");
var doc = app.defaultDocument;
var tasks = doc.flattenedTasks.whose({completed: false});
for (var i = 0; i < tasks.length; i++) {
report.push(toJson(tasks[i]));
}
JSON.stringify(report);
function toJson(task) {
var note = task.note();
var res = {};
res.name = task.name();
res.id = task.id();
res.completed = task.completed();
res.flagged = task.flagged();
if (note) {
res.note = note.text;
} else {
res.note = "";
}
res.project = task.container().name();
if (res.project == "OmniFocus") {
res.project = "Inbox";
}
res.inInbox = task.inInbox()
if (task.context()) {
res.context = task.context().name();
}
res.creationDate = task.creationDate();
res.modificationDate = task.modificationDate();
if (task.deferDate()) {
res.deferDate = task.deferDate();
}
if (task.dueDate()) {
res.dueDate = task.dueDate();
}
if (task.completionDate()) {
res.completionDate = task.completionDate();
}
return res;
}
@cescoffier
Copy link
Author

3 examples of JavaScript automation tasks dealing with Omnifocus.

retrieve-tasks-from-omnifocus

Retrieve all tasks from Omnifocus and export them as JSON. The JSON output is written the "out" stream of the process, so it can be easily read by a Java program or whatever.

Be aware that the export take around 1 second per task.... Update the query to make it faster.

put-task-on-hold

This script is launched from the command line (osascript put-tasks-on-hold.scpt $task_id).
It searches for the Waiting context (to update if your on-hold context is named differently). Then it looks for the task with the given id (argument) and set the context to the found one.

complete-task

This script is launched from the command line (osascript put-tasks-on-hold.scpt $task_id).
Search for the task with the given id and mark it as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment