×

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 script, 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.

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
Add Specific Date/Time Notification to Selected Tasks (Absolute)
 

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) }) }

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:

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
Remove Absolute Notifications from Selected Task
 

var task = document.windows[0].selection.tasks[0] task.notifications.forEach(notif =>{ if (notif.kind === Task.Notification.Kind.Absolute){ task.removeNotification(notif) } })

The Task.Notification Class

Here are the properties of an instance of the Task.Notification class:

Task.Notification.Kind

Notifications Timed Relative to Task Due Date

To set a notification relative to the due date (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 to occur.

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
Notify 5-minutes Before Due
 

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%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
Remove Relative Notifications from Selected Task
 

var task = document.windows[0].selection.tasks[0] task.notifications.forEach(notif =>{ if (notif.kind === Task.Notification.Kind.DueRelative){ task.removeNotification(notif) } })

Add Standard Notifications

The following script will add these notifications to the selected task, after removing any existing notifications:

omnifocus://localhost/omnijs-run?script=var%20task%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Etasks%5B0%5D%0Aif%28task%20%26%26%20task%2EdueDate%20%21%3D%3D%20null%29%7B%0A%09task%2Enotifications%2EforEach%28notif%20%3D%3E%20task%2EremoveNotification%28notif%29%29%0A%09var%20dateObj%20%3D%20task%2EdueDate%0A%09%2F%2F%20day%20before%0A%09task%2EaddNotification%28%2D86400%29%0A%09%2F%2F%20morning%20of%0A%09var%20dc%20%3D%20Calendar%2Ecurrent%2EdateComponentsFromDate%28dateObj%29%0A%09dc%2Ehour%20%3D%209%20%2F%2F%209%3A00AM%0A%09dc%2Eminute%20%3D%200%0A%09var%20notificationDateObj%20%3D%20Calendar%2Ecurrent%2EdateFromDateComponents%28dc%29%0A%09task%2EaddNotification%28notificationDateObj%29%0A%09%2F%2F%20hour%20before%0A%09task%2EaddNotification%28%2D3600%29%0A%7D
Set to Standard Notifications
  

var task = document.windows[0].selection.tasks[0] if(task && task.dueDate !== null){ task.notifications.forEach(notif => task.removeNotification(notif)) var dateObj = task.dueDate // day before task.addNotification(-86400) // morning of var dc = Calendar.current.dateComponentsFromDate(dateObj) dc.hour = 9 // 9:00AM dc.minute = 0 var notificationDateObj = Calendar.current.dateFromDateComponents(dc) task.addNotification(notificationDateObj) // hour before task.addNotification(-3600) }

NOTE: the previous script uses the Date Components class (LINK) of the Calendar class to derive a relative date.