deferDate property and date issue

The following makes the automation console spit an error at >>: Property Task.deferDate requires a Date, but was passed value of type Number - and for the life of me I can’t figure out why if I am passing a Date.

I am trying to defer an item by 7 days.

[…]
var d = new Date()
var defer = d.setDate(d.getDate() + 7)
[…]

dupTasks[0].deferDate = defer;
[…]

You should be able to paste and enter something like this in the OmniFocus automation console:

(() => {
    "use strict";

    // main :: IO ()
    const main = () => {
        const
            now = new Date(),
            sevenDaysHence = new Date(
                now.setDate(7 + now.getDate())
            );

        const
            selectedTasks = document.windows[0].selection.tasks,
            maybeTask = 0 < selectedTasks.length ? (
                selectedTasks[0]
            ) : null;

        const message = maybeTask ? (
            // In OmniFocus,
            maybeTask.deferDate = sevenDaysHence,

            // and in JavaScript.
            `'${maybeTask.name}' deferred to ` + (
                `${iso8601Local(maybeTask.deferDate)}`
            )
        ) : "No tasks selected";

        return (
            // In Console,
            console.log(message),

            // and in the JavaScript interpreter.
            message
        );
    };

    // --------------------- GENERIC ---------------------

    // iso8601Local :: Date -> String
    const iso8601Local = dte =>
        new Date(dte - (6E4 * dte.getTimezoneOffset()))
        .toISOString();

    return main();
})();
2 Likes

Very nicely put. It’s too bad that a lot of the plugin examples on omni-automation have code clean as yours.

I guess my mistake above is that I needed ‘defer’ to be defined as a new Date of the d.setDate.

var defer = new Date ( d.setDate(d.getDate() + 7) )

1 Like

Exactly.

const defer = new Date ( d.setDate(d.getDate() + 7) )

And, incidentally, we get more helpful messages back from the JavaScript interpreter if we use const by default and let for the rarer occasions when we really want and need to reassign a name to a different value.

var has tricky behaviour that can trip us up, is no longer needed, and is now generally deprecated. See, for example:

[no-var - ESLint Rules]( https://eslint.org/docs/rules/no-var )

The author of omni-automation.com seems to have a dirty habit of polluting the global name space by assigning names in it, and uses var to suppress the helpful error messages from JavaScript which this leads to :-)

glad you understood that I meant: do not have code clean as yours

1 Like

Right Date is a built-in type. And you can do date arithmetic, sorting, and formatting with it.