The Window class contains the DocumentWindow subclass that displays the controls and views for accessing and altering the data in the OmniFocus backing database.

The scripting implementation for the Window class contains no properties, and one function.

if ( > 1){,index) => { if (index != 0){ console.log("close " + index) win.close() } }) }

DocumentWindow Instance Properties

Here are the properties of the DocumentWindow class:

A script that sets the value of the focus property:

project1 = projectNamed("My Top-Level Project") project2 = projectNamed("Single-Action Project")[0].focus = [project1, project2]

A script that sets the value of the perspective property:

// get the current perspective object[0].perspective //--> [object Perspective.BuiltIn: Projects] // set the perspective[0].perspective = Perspective.BuiltIn.Inbox

A script that sets the values of the perspective and focus properties:

droppedProjects = new Array() library.apply(function(item){ if (item instanceof Project && item.status === Project.Status.Dropped){ droppedProjects.push(item) } }) if(droppedProjects){[0].perspective = Perspective.BuiltIn.Projects[0].focus = droppedProjects }

NOTE: the value of the content and sidebar properties of the Window class are ContentTree and SidebarTree types that inherit from the Tree class, which is the same class that is used to organize and control the display of items in OmniOutliner. Currently these node trees are implemented only on macOS, and are not functional on iOS or iPadOS.

var tasks = new Array()[0].content.rootNode.apply((item) => { if (item.object instanceof Task) { tasks.push(item); } }) if(tasks != []){[0] }

Here's a variation of the previous script that will select all of the tasks and/or projects in the current content view:

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "", "version": "1.0", "description": "This action will select all of the tasks and/or projects in the current content view. (macOS only)", "label": "Select Items in View", "shortLabel": "Select Items" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code // selection options: tasks, projects, folders, tags var tasksCheckbox = new Form.Field.Checkbox( "shouldSelectTasks", "Tasks", false ) var projectsCheckbox = new Form.Field.Checkbox( "shouldSelectProjects", "Projects", false ) var inputForm = new Form() inputForm.addField(tasksCheckbox) inputForm.addField(projectsCheckbox) var formPromise ="Select in content view:","Continue") inputForm.validate = function(formObject){ var shouldSelectTasks = formObject.values['shouldSelectTasks'] var shouldSelectProjects = formObject.values['shouldSelectProjects'] return ((shouldSelectTasks || shouldSelectProjects) ? true:false) } formPromise.then(function(formObject){ var shouldSelectTasks = formObject.values['shouldSelectTasks'] var shouldSelectProjects = formObject.values['shouldSelectProjects'] var items = new Array()[0].content.rootNode.apply((item) => { if (shouldSelectTasks && item.object instanceof Task) { items.push(item); } if (shouldSelectProjects && item.object instanceof Project) { items.push(item); } }) if(items != []){[0] } }) }); action.validate = function(selection, sender){ // validation code // selection options: tasks, projects, folders, tags return (Device.current.mac) }; return action; })();

Instance Functions

IMPORTANT: Since the use these functions require a specific perspective be displayed, the example scripts use a Timer function to allow the window time to change views.

var allProjects = flattenedProjects var targetName = "Project A" var targetProject = null for (i = 0; i < allProjects.length; i++){ var project = allProjects[i] if ( === targetName){ targetProject = project break } } if(targetProject){[0].perspective = Perspective.BuiltIn.Projects Timer.once(1,function(timer){[0].selectObjects([targetProject]) }) }[0].perspective = Perspective.BuiltIn.Forecast Timer.once(1,function(timer){ var now = new Date() var today = Calendar.current.startOfDay(now) var dc = new DateComponents() = 7 var targetDate = Calendar.current.dateByAddingDateComponents(today,dc) var fday =[0].forecastDayForDate(targetDate)[0].selectForecastDays([fday]) })
omnifocus://localhost/omnijs-run?script=try%7Bdocument%2Ewindows%5B0%5D%2Eperspective%20%3D%20Perspective%2EBuiltIn%2EForecast%0ATimer%2Eonce%281%2Cfunction%28timer%29%7B%0A%09var%20now%20%3D%20new%20Date%28%29%0A%09var%20today%20%3D%20Calendar%2Ecurrent%2EstartOfDay%28now%29%0A%09var%20dc%20%3D%20new%20DateComponents%28%29%0A%09dc%2Eday%20%3D%207%0A%09var%20targetDate%20%3D%20Calendar%2Ecurrent%2EdateByAddingDateComponents%28today%2Cdc%29%0A%09var%20fday%20%3D%20document%2Ewindows%5B0%5D%2EforecastDayForDate%28targetDate%29%0A%09document%2Ewindows%5B0%5D%2EselectForecastDays%28%5Bfday%5D%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D[0].perspective = Perspective.BuiltIn.Forecast Timer.once(1,function(timer){ var now = new Date() var today = Calendar.current.startOfDay(now) var dc = new DateComponents() var fdays = new Array() var fday, targetDate, i for (i = 0; i < 7; i++) { = i + 1 targetDate = Calendar.current.dateByAddingDateComponents(today,dc) fday =[0].forecastDayForDate(targetDate) fdays.push(fday) }[0].selectForecastDays(fdays) })

NOTE: These examples use properties and functions of the shared Calendar class to calculate dates.

Document Selection

A document’s Selection object belongs to the Window class, which in turn, belongs to the parent implied document. The Selection class includes properties whose values are arrays of references to the specified object types selected in the window: projects, folders, tags, tasks, and a special property for returning all selected items: allObjects

Document > Window > Selection[0].selection.database[0].selection.folders[0].selection.projects[0].selection.tags[0].selection.tasks[0].selection.window[0].selection.allObjects

For example, here’s a simple script for getting the titles of the selected tasks:

selectedTasks =[0].selection.tasks taskTitles = => console.log(taskTitles)

Move selected projects into new top-level project group:

items =[0].selection.projects if(items.length > 0){moveSections(items, new Folder("Project Group"))}

DatabaseDocument Class

The DatabaseDocument class is used to create new windows or tabs containing views of the database content.

var windowPromise = document.newWindow() windowPromise.then(function(win){ win.perspective = Perspective.BuiltIn.Projects }) windowPromise.catch(function(error){ console.error(error.message) })
var windowPromise = document.newTabOnWindow([0]) windowPromise.then(function(win){ win.perspective = Perspective.BuiltIn.Forecast }) windowPromise.catch(function(error){ console.error(error.message) })

Copy Tags Between Selected Tasks

The following Omni Automaton plug-in uses the document selection, the addTags(…) function of the Task class, and interactive Action Forms, to copy the tags of a chosen task to the other tasks selected in the OmniFocus window.

To use, select both the tasks to receive the tags, and the task containing the set of tags to be copied, and run the action.

An example plug-in for copying tags between tasks:

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.copy-tags-between-selected-tasks", "version": "1.5", "description": "Append the tags of the chosen task to the other selected tasks.", "label": "Copy Tags between Selected Tasks", "shortLabel": "Copy Tags" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // selection options: tasks, projects, folders, tags var selectedTasks = selection.tasks var taskNames ={ return }) var inputForm = new Form() taskNames.forEach(function(taskName,index){ checkboxSwitch = new Form.Field.Checkbox( String(index), taskName, false ) inputForm.addField(checkboxSwitch) }) formPrompt = "Select the single task to copy tags from:" formPromise =,"Continue") inputForm.validate = function(formObject){ resultArray = Object.values(formObject.values) var count = 0; for (var i = 0; i < resultArray.length; i++) { if (resultArray[i] == true){count = count + 1} } return ((count == 1) ? true : false) } formPromise.then(function(formObject){ keys = Object.keys(formObject.values) for (var i = 0; i < keys.length; i++) { k = keys[i] if (formObject.values[k] == true){ var chosenTask = selectedTasks[Number(k)] var chosenTaskName = taskNames[Number(k)] } } str1 = "Copy the tags from “" + chosenTaskName + "” to the other selected tasks?" str2 = "Tags will be added to those tags already applied to the other selected tasks." alertMsg = str1.concat("\n\n",str2) var alert = new Alert("CONFIRMATION:", alertMsg) alert.addOption("Continue") alert.addOption("Cancel") var alertPromise = alertPromise.then(function(result){ if (result == 0){ sourceTags = chosenTask.tags selectedTasks.forEach(function(task){ task.addTags(sourceTags) }) } }) }) }); action.validate = function(selection, sender){ // selection options: tasks, projects, folders, tags return (selection.tasks.length > 1) }; return action; })();

Displaying (selecting) an Item in the Window

While the focus property of the DocumentWindow class cna be used to display specific folders and projects, currently the Omni Automation implementation in OmniFocus does not offer a script function for setting the contents of the Selection class to a specific task. However, you can incorporate the built-in URL support of OmniFocus with scripts to create and execute URLs that will cause specified items to be displayed in the document window.

To create a URL for displaying (selecting) an OmniFocus object, the primaryKey property of the ObjectIdentifer class is appended to the standard OmniFocus URL task protocol, as in the following Omni Automation action that displays the parent project of a selected task:

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.display-host-project", "version": "1.0", "description": "This action will display the host project of the selected task.", "label": "Display Host Project", "shortLabel": "Display Project" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code // selection options: tasks, projects, folders, tags task = selection.tasks[0] project = task.containingProject if(project){ projID = urlStr = "omnifocus:///task/" + projID URL.fromString(urlStr).call(reply => {}) } }); action.validate = function(selection, sender){ // validation code // selection options: tasks, projects, folders, tags return (selection.tasks.length === 1) }; return action; })();

 01-10  The Action metadata.

 11-31  The main function containing the action code.

 24-28  The action validation routine will enable the action in the Automation menu when a single task is selected.

 12-22  The function for creating the action, which is passed the selection object by default.

 15  The currently selected task is the first (and only) item in the selection array.

 16  Use the containingProject property of the Task class to get a reference to the parent project. If the selected task is not included in a project, no value (undefined) will be returned.

 17-21  A conditional for handling the project object.

 18  The value of the primaryKey property is a unique identifier string.

 19  Append the identifier string to URL string targeting the OmniFocus application.

 20  Use the fromString(…) method of the URL class to convert the string into a URL object, and then execute the url by appending the open() function to the result.