×

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.

Assign a “Morning Due” Notification

Here is a script that will assign the chosen morning due notification to the selected task or project:

Assign a “Morning Due” Notification


(async () => { var sel = document.windows[0].selection var selCount = sel.tasks.length + sel.projects.length if(selCount === 1){ if (sel.tasks.length === 1){ var item = sel.tasks[0] if (!item.dueDate){ throw {name:"No Due Date", message: "The selected task has no assigned due date."} } } else { var item = sel.projects[0] if (!item.dueDate){ throw {name:"No Due Date", message: "The selected project has no assigned due date."} } } var notifcationTimes = ["5:00", "5:15", "5:30", "5:45", "6:00", "6:15", "6:30", "6:45", "7:00", "7:15", "7:30", "7:45", "8:00", "8:15", "8:30", "8:45", "9:00", "9:15", "9:30", "9:45", "10:00"] var menuIndexes = notifcationTimes.map((item, index) => index) var notificationOptionsMenu = new Form.Field.Option( "menuID", null, menuIndexes, notifcationTimes, 0 ) var inputForm = new Form() inputForm.addField(notificationOptionsMenu) var formPrompt = "Choose a morning time:" var buttonTitle = "Continue" var formObject = await inputForm.show(formPrompt, buttonTitle) var index = formObject.values["menuID"] var timeString = notifcationTimes[index] var hoursMinutes = timeString.split(":") var dc = Calendar.current.dateComponentsFromDate(item.dueDate) dc.hour = Number(hoursMinutes[0]) dc.minute = Number(hoursMinutes[1]) dc.second = 0 var notifDateObj = Calendar.current.dateFromDateComponents(dc) item.addNotification(notifDateObj) } else { throw {name: "Selection Issue", message: "Please select a single project or task with an assigned due date."} } })().catch(err => { if(err.name !== "Error"){ new Alert(err.name, err.message).show() } })

Clear Notifications from Projects and/or Tasks

Clears notifications from the selected tasks and/or projects:

Clear All Notifications


try { var sel = document.windows[0].selection var selCount = sel.tasks.length + sel.projects.length if(selCount > 0){ sel.databaseObjects.forEach(item => { if(item instanceof Project){ item.task.notifications.forEach(notif => item.task.removeNotification(notif)) } else if (item instanceof Task){ item.notifications.forEach(notif => item.removeNotification(notif)) } }) } else { throw {name: "Selection Issue", message: "Please select projects and/or tasks."} } } catch(err){ new Alert(err.name, err.message).show() }