Finding|Processing Items with the Apply(…) Function

It is common for scripts to perform their automation tasks by first locating a set of objects that meet a specified set of criteria, and then process the found items. This page contains examples of using the apply(…) function to process object hierarchies to locate and process items in the OmniFocus database.

The apply(…) function can be called the database properties: inbox, library, and tags and on instances of the Folder, Tag, and Task classes.

folderNamed("Fall Festival").apply(item => { if (item instanceof Folder){console.log("F • " + item.name)} if (item instanceof Project){console.log("P • " + item.name)} })

Inbox Tasks

To iterate all tasks in the Inbox hierarchy — even those tasks within other tasks — use the apply(…) function:

inbox.apply((task)=>{ if (task.taskStatus === Task.Status.Available){ // processing statements go here } })
taskNames = new Array() inbox.apply(task => taskNames.push(task.name)) console.log(taskNames)
omnifocus://localhost/omnijs-run?script=try%7BtaskNames%20%3D%20new%20Array%28%29%0Ainbox%2Eapply%28%28task%29%3D%3E%7B%0A%09taskNames%2Epush%28task%2Ename%29%0A%7D%29%0Aconsole%2Elog%28taskNames%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

All Inbox tasks matching a specified name:

var targetTaskName = "SUB-TASK" var targetTasks = new Array() inbox.apply(function(task){ if (task.name == targetTaskName ){ targetTasks.push(task) } }) console.log(targetTasks)

ApplyResult Class

By default, the apply(…) method when called on a database property with hierarchical content, will iterate every item of the heirarchy, processing each item with the processing code you provide. Optionally, you can use the properties of the ApplyResult class to indicate if certain types of iterated items should be ignored, or if the iteration process should stop.

Here’s an example script that returns the ApplyResult.Stop property to identify the first instance of an Inbox task specified by name:

var targetTaskName = "My Secondary Task" var targetTask = null inbox.apply(function(task){ // stop the iteration process when a match is found if (task.name == targetTaskName ){ targetTask = task return ApplyResult.Stop } }) if(targetTask){console.log(targetTask)}

Here’s a variation of the previous script, for locating a task in the Inbox hierarchy matched by identifier:

var targetTaskID = "jm86R3Q3F3I" var targetTask = null inbox.apply(function(task){ // stop the iteration process when a match is found if (task.id.primaryKey == targetTaskID){ targetTask = task return ApplyResult.Stop } }) if(targetTask){console.log(targetTask)}
var targetTasks = new Array() inbox.apply(function(task){ targetTasks.push(task) return ApplyResult.SkipChildren }) console.log(targetTasks)
var targetTasks = new Array() inbox.apply(function(task){ targetTasks.push(task) return ApplyResult.SkipPeers }) console.log(targetTasks)

Using the SkipChildren property to iterate just the top-level Inbox items:

var targetTasks = new Array() inbox.apply(function(task){ targetTasks.push(task) return ApplyResult.SkipChildren }) console.log(targetTasks)

Folders and Projects

Aenean lacinia bibendum nulla sed consectetur. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nulla vitae elit libero, a pharetra augue. Curabitur blandit tempus porttitor. Vestibulum id ligula porta felis euismod semper.

library.apply(function(item){ if (item instanceof Project){ console.log(item.name) } })
library.apply(function(item){ if (item instanceof Folder){ console.log(item.name) } })

A script example using the ApplyResult.Stop property to get the tasks of a project specified by name:

var targetProjectName = "My Project" var targetProject = null library.apply(function(item){ // stop the iteration process when a match is found if (item instanceof Project && item.name == targetProjectName){ targetProject = item return ApplyResult.Stop } }) if(targetProject){ tasks = targetProject.task.children }
var targetTaskID = "jm86R3Q3F3I" var targetTask = null inbox.apply(function(task){ // stop the iteration process when a match is found if (task.id.primaryKey == targetTaskID){ targetTask = task return ApplyResult.Stop } }) if(targetTask){console.log(targetTask)}

Locating projects within the entire hierarchy of a specified folder:

var projectList = new Array() folderNamed("Fall Festival").apply(item => { if (item instanceof Project){projectList.push(item)} })

Tags

The apply(…) function is also useful for iterating the entire heirarchy of tags, including tag groups.

targetTagName = "Michigan" var targetTag = null tags.apply(function(tag){ if(tag.name == targetTagName){ targetTag = tag return ApplyResult.Stop } }) tag = targetTag || new Tag(targetTagName)

Project Tasks

You can use the hasChildren and children properties of the root task to access an array of all of the tasks contained in the project, as in this script that gather reference4s to all tasks in all projects:

var masterTaskArray = new Array() library.apply(function(item){ if (item instanceof Project){ if (item.task.hasChildren){ masterTaskArray.push(item.task.children) } } }) console.log(masterTaskArray)

Add an iteration of the Inbox, and here’s a script for getting references to ALL TASKS in the database:

var masterTaskArray = new Array() library.apply(function(item){ if (item instanceof Project){ if (item.task.hasChildren){ masterTaskArray.push(item.task.children) } } }) inbox.apply((task)=>{ masterTaskArray.push(task) }) console.log(masterTaskArray)
UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER