How to get going with javascript and Omnifocus?

FWIW if you wanted to do that kind of thing in Script Editor (JS makes use of $ and various other things which don’t play well in the shell, even with a unix heredoc), you could trying writing something like the snippet below.

You will notice that it involves a different object structure (API) from the currently maintained in-app OmniJS JSContext that you can explore with the Console and omnijs plugins.

In Script Editor, with the language selector at top-left set to JavaScript:

Expand disclosure triangle to view JS Source
(() => {
    "use strict";

    const OmniFocus = Application("OmniFocus.app");
    const
        doc = OmniFocus.defaultDocument,
        inbox = doc.inboxTasks;

     OmniFocus.activate();

    console.log(`inbox length: ${inbox.length}`);
    console.log(
        `Flattened projects length: ${doc.flattenedProjects.length}`
    );
})();

And if you really wanted to do it in the shell, you would need

  • a heredoc to cope with a multi-line script,
  • escaped dollars and backticks, and
  • a return expression in lieu of console.log

Expand disclosure triangle to view shell script
osascript -l JavaScript <<JXA_END 2>/dev/null
(() => {
    "use strict";

    const
        OmniFocus = Application("OmniFocus.app"),
        doc = OmniFocus.defaultDocument,
        inbox = doc.inboxTasks;

    OmniFocus.activate();

    return (
        [
            \`inbox length: \${inbox.length}\`,
            \`Flattened projects length: \${doc.flattenedProjects.length}\`
        ].join("\n")
    );
})();
JXA_END
3 Likes