Task Notifications

Apple devices offer the ability to display alerts or notifications regarding current and upcoming events. OmniFocus uses the native notification abilities of the Apple device to display user-defined task-related notifications. Using Omni Automation, notifications can be created, assigned, and removed from tasks.

Notification-related properties and functions of the Task class:

These properties and functions are used in conjunction with properties of the Notification class to assign and remove notifications to and from tasks.

In this first example, a notifications designed to be triggered three days from the current time, are assigned to the selected tasks. NOTE: properties and functions of the Calendar class are used to derive the future date/time.

var tasks = document.windows[0].selection.tasks if (tasks != undefined){ // derive the notification date/time var now = new Date() var dc = new DateComponents() dc.day = 3 var targetDate = Calendar.current.dateByAddingDateComponents(now,dc) tasks.forEach(task =>{ // supplying a Date object creates an absolute notification task.addNotification(targetDate) }) }
omnifocus://localhost/omnijs-run?script=try%7Bvar%20tasks%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Etasks%0Aif%20%28tasks%29%7B%0A%09%2F%2F%20derive%20the%20notification%20date%2Ftime%0A%09var%20now%20%3D%20new%20Date%28%29%0A%09var%20dc%20%3D%20new%20DateComponents%28%29%0A%09dc%2Eday%20%3D%203%0A%09var%20targetDate%20%3D%20Calendar%2Ecurrent%2EdateByAddingDateComponents%28now%2Cdc%29%0A%09tasks%2EforEach%28task%20%3D%3E%7B%0A%09%09%2F%2F%20supplying%20a%20Date%20object%20creates%20an%20absolute%20notification%0A%09%09task%2EaddNotification%28targetDate%29%0A%09%7D%29%0A%7D%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

Notifications are defined as “absolute” when a fixed date/time is used when creating the notification. The following example script demonstrates how to remove all absolute notifications from the selected task:

var task = document.windows[0].selection.tasks[0] task.notifications.forEach(notif =>{ if (notif.kind === Task.Notification.Kind.Absolute){ task.removeNotification(notif) } })
omnifocus://localhost/omnijs-run?script=try%7Bvar%20task%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Etasks%5B0%5D%0Atask%2Enotifications%2EforEach%28notif%20%3D%3E%7B%0A%09if%20%28notif%2Ekind%20%3D%3D%3D%20Task%2ENotification%2EKind%2EAbsolute%29%7B%0A%09%09task%2EremoveNotification%28notif%29%0A%09%7D%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

Task.Notification

Task.Notification.Kind

Notifications Timed Relative to Task Due Date

To set a notification relative to the due data (before or after), provide a positive or negative integer indicating the number of seconds before (negative) or after (positive) to due date/time for the notification is to occur.

var tasks = document.windows[0].selection.tasks if (tasks != undefined){ var relativeOffset = -300 // seconds tasks.forEach(task => { if (task.dueDate){ task.addNotification(relativeOffset) } }) }
omnifocus://localhost/omnijs-run?script=try%7Bvar%20tasks%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Etasks%0Aif%20%28tasks%20%21%3D%20undefined%29%7B%0A%09var%20relativeOffset%20%3D%20%2D300%20%2F%2F%20seconds%0A%09tasks%2EforEach%28task%20%3D%3E%20%7B%0A%09%09if%20%28task%2EdueDate%29%7B%0A%09%09%09task%2EaddNotification%28relativeOffset%29%0A%09%09%7D%0A%09%7D%29%0A%7D%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
var task = document.windows[0].selection.tasks[0] task.notifications.forEach(notif =>{ if (notif.kind === Task.Notification.Kind.DueRelative){ task.removeNotification(notif) } })
omnifocus://localhost/omnijs-run?script=try%7Bvar%20task%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Etasks%5B0%5D%0Atask%2Enotifications%2EforEach%28notif%20%3D%3E%7B%0A%09if%20%28notif%2Ekind%20%3D%3D%3D%20Task%2ENotification%2EKind%2EDueRelative%29%7B%0A%09%09task%2EremoveNotification%28notif%29%0A%09%7D%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

DISCLAIMER