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.

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

Tags

Donec ullamcorper nulla non metus auctor fringilla. Donec id elit non mi porta gravida at eget metus. Nulla vitae elit libero, a pharetra augue. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

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