How to programmatically navigate the Forecast perspective?

Hey all,

I have two scripts for programmatically going to tomorrow and yesterday in the Forecast perspective.

Below is the code for showing tomorrow.

Now I’d like to generalize this so that I can go to selectedDate+1 (move one day next from selected day).

The script below starts always from todays date and navigates to tomorrow or yesterday:

var nowTomorrow = new Date()

But I’d like to be able to navigate +1 day from any selected date. So how can I find out which day is selected, programmatically? Could not find anything in the API docu.

/*{
    "type": "action",
    "targets": ["omnifocus"],
    "author": "Udo Leiteritz",
    "identifier": "com.omni-automation.of.show-forecast-tomorrow",
    "version": "1.0",
    "description": "Selects past due and tomorrow in the forecast day",
    "label": "Show Forecast Tomorrow",
    "shortLabel": "Forecast Tomorrow"
}*/
(() => {
    var action = new PlugIn.Action(function(selection, sender){
        var win = document.windows[0]
        win.perspective = Perspective.BuiltIn.Forecast
        
        var nowTomorrow = new Date()
        nowTomorrow.setDate(nowTomorrow.getDate() + 1)
        var tomorrow = Calendar.current.startOfDay(nowTomorrow)
        foreCastTomorrow = win.forecastDayForDate(tomorrow)
        
        var today = Calendar.current.startOfDay(new Date())
        foreCastPastDue = win.forecastDayForDate(today)

        //win.selectForecastDays([foreCastPastDue, foreCastTomorrow])
        win.selectForecastDays([foreCastTomorrow])
    });

    action.validate = function(selection, sender){
        return true
    };
    
    return action;
})();

The selected forecast day is the sidebar’s selection:

document.windows[0].sidebar.selectedNodes[0].object.date

Other forecast days in the sidebar will be listed as children of the sidebar’s root node:

document.windows[0].sidebar.rootNode.children

Hope this helps!

1 Like

Hey Key, thank you very much! That helped.

Here’s the code, for anyone interested:

Navigate to previous day in Forecast perspective

/*{
    "type": "action",
    "targets": ["omnifocus"],
    "author": "Ugur",
    "identifier": "com.omni-automation.of.navigate-forecast-previous-day",
    "version": "1.0",
    "description": "Navigates one day back in the forecast view",
    "label": "Navigate Forecast Previous Day",
    "shortLabel": "Previous Day"
}*/
(() => {
    var action = new PlugIn.Action(function(selection, sender){
        var win = document.windows[0]
        var currentDate = win.sidebar.selectedNodes[0].object.date

        // Check if the year is already 0000
        if (currentDate.getFullYear() === 0) {
            // Do not navigate further back
            return
        }

        win.perspective = Perspective.BuiltIn.Forecast
        
        var previousDate = new Date(currentDate)
        previousDate.setDate(previousDate.getDate() - 1)
        var previousDay = Calendar.current.startOfDay(previousDate)
        forecastPreviousDay = win.forecastDayForDate(previousDay)
        
        win.selectForecastDays([forecastPreviousDay])
    });

    action.validate = function(selection, sender){
        return true
    };
    
    return action;
})();

Navigate to next day in Forecast

/*{
    "type": "action",
    "targets": ["omnifocus"],
    "author": "Ugur",
    "identifier": "com.omni-automation.of.navigate-forecast-next-day",
    "version": "1.0",
    "description": "Navigates one day forward in the forecast view",
    "label": "Navigate Forecast Next Day",
    "shortLabel": "Next Day"
}*/
(() => {
    var action = new PlugIn.Action(function(selection, sender){
        var win = document.windows[0]
        win.perspective = Perspective.BuiltIn.Forecast
        
        var currentDate = win.sidebar.selectedNodes[0].object.date
        var nextDate

        // Check if the year is 0000 and set the date to today if it is
        if (currentDate.getFullYear() === 0) {
            nextDate = new Date()
        } else {
            nextDate = new Date(currentDate)
            nextDate.setDate(nextDate.getDate() + 1)
        }

        var nextDay = Calendar.current.startOfDay(nextDate)
        forecastNextDay = win.forecastDayForDate(nextDay)
        
        win.selectForecastDays([forecastNextDay])
    });

    action.validate = function(selection, sender){
        return true
    };
    
    return action;
})();
1 Like