Script is unavailable unless console is opened

I wrote a simple script (copied below) that increases the frequency of a repeatable task. The script works fine, but it is not available when I first open OmniFocus on my Mac, it is only available after I open the Console window. I can close the console window and then script still remains available.

Any idea what I should change to avoid having to open the console?

(() => {
    var action = new PlugIn.Action(function(selection) {
        const task = selection.tasks[0];
        const oldRuleString = task.repetitionRule.ruleString;
        const repetitionMethod = task.repetitionRule.method;
        var newRuleString = "";
        if (oldRuleString.indexOf("INTERVAL") > -1) {
            const indexOfLastEqual = oldRuleString.lastIndexOf('=');
            const oldInterval = oldRuleString.substring(indexOfLastEqual + 1);
            const newInterval = Math.floor(oldInterval*0.9);
            newRuleString = oldRuleString.replace(oldInterval, newInterval);
        }
        else {
            newRuleString = "FREQ=HOURLY;INTERVAL=2"
        }
        const newRule = new Task.RepetitionRule(newRuleString, repetitionMethod);
        task.repetitionRule = newRule;
        task.markComplete();
    });

    action.validate = function(selection){
        // Only works if there is a single repeatable task selected
        return (selection.tasks.length == 1) && selection.tasks[0].repetitionRule;
    };
    return action;
})();

I can reproduce that behaviour. I found that, changing the right side of this conjunction…

(selection.tasks.length == 1) && selection.tasks[0].repetitionRule;

to

selection.tasks.some(x => null !== x.repetitionRule);

solves the problem.

In English, that predicate will return true in case there is some task whose repetitionRule property is not null.

P.S.: On a side note, you are missing manifest comment. Perhaps, it is present on your local copy.

1 Like

This post was flagged by the community and is temporarily hidden.

Thanks!
That does work, I made the change to work with multiple tasks as well to avoid the short circuiting entirely.
I’m including the simple manifest in this case as well.

/*{
    "author": "Jerome Lefebvre",
    "targets": ["omnifocus"],
    "type": "action",
    "identifier": "com.mycompany.test",
    "version": "0.2",
    "description": "Increase the frequency of the selected repetable tasks",
    "label": "Increase Frequency",
    "mediumLabel": "Increase tasks frequency",
    "paletteLabel": "Increase tasks frequency"
}*/
(() => {
    var action = new PlugIn.Action(function(selection) {
        selection.tasks.forEach(task => {
            const oldRuleString = task.repetitionRule.ruleString;
            const repetitionMethod = task.repetitionRule.method;
            var newRuleString = "";
            if (oldRuleString.indexOf("INTERVAL") > -1) {
                const indexOfLastEqual = oldRuleString.lastIndexOf('=');
                const oldInterval = oldRuleString.substring(indexOfLastEqual + 1);
                const newInterval = Math.floor(oldInterval*0.9);
                newRuleString = oldRuleString.replace(oldInterval, newInterval);
            }
            else {
                newRuleString = "FREQ=HOURLY;INTERVAL=2"
            }
            const newRule = new Task.RepetitionRule(newRuleString, repetitionMethod);
            task.repetitionRule = newRule;
            task.markComplete();
        });
    });

    action.validate = function(selection){
        // Only works if all tasks are repeatable actions
        return selection.tasks.all(x => null !== x.repetitionRule);
    };
    return action;
})();

You’re welcome.

What’s this .all method ? Where it is documented ?

Sorry, I wanted to change some to something that would check the condition for all selected tasked, assume all would work, but I really meant to have every.

1 Like

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.