×

Document

By default, OmniFocus is designed to use a single data source whose content is synced across your devices and is even accessible using a web-based HTML interface (browser) on non-Apple hardware.

In OmniFocus, a “document” provides a visual interface to the synced database that manages all of your projects, tasks, schedules, and information.

Document Properties

Here are the scripting properties of a document:

document.writableTypes.join('\n')
Get the Writable Types


document.writableTypes.join('\n') //--> "com.omnigroup.omnifocus.filetype.ofocus com.omnigroup.omnifocus2.export-filetype.plain-text com.omnigroup.omnifocus2.export-filetype.html com.omnigroup.omnifocus2.export-filetype.comma-separated-values com.omnigroup.omnifocus2.export-filetype.comma-separated-values-unicode com.omnigroup.omnifocus.filetype.ofocus-backup"

New Documents

Omni Automation scripts have the ability to create and manipulate new documents using the class and instance functions of the Document class. As OmniFocus is optimized for use with a single data source, the following functions are provided for theoretical and archival purposes.

Class Functions

omnifocus://localhost/omnijs-run?script=try%7BDocument%2EmakeNewAndShow%28function%28newDoc%29%7B%0A%09newDoc%2Ewindows%5B0%5D%2Eperspective%20%3D%20Perspective%2EBuiltIn%2EInbox%0A%09var%20db%20%3D%20newDoc%2Ewindows%5B0%5D%2Eselection%2Edatabase%0A%09var%20tsk%20%3D%20new%20Task%28%22NEW%20TASK%22%2Cdb%2Einbox%2Ebeginning%29%0A%7D%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
Show New Document
 

Document.makeNewAndShow(function(newDoc){ newDoc.windows[0].perspective = Perspective.BuiltIn.Inbox var db = newDoc.windows[0].selection.database var tsk = new Task("NEW TASK",db.inbox.beginning) })

Instance Functions

The supportive functions that can be called on an instance of the Document class.

Undo and Redo
 

if(document.canUndo){document.undo()} if(document.canRedo){document.redo()}

Opening an OmniFocus Document

Existing OmniFocus documents are opened using the openDocument(…) function of the Application class.

The following example script uses the FilePicker and FileType classes to display a file picker dialog to the user, allowing them to choose the OmniFocus document to open and process.

Open Document and Process InBox Tasks


var picker = new FilePicker() picker.folders = false picker.multiple = false aFileType = new FileType("com.omnigroup.omnifocus.filetype.ofocus") picker.types = [aFileType] var pickerPromise = picker.show() pickerPromise.then(urlsArray => { fileURL = urlsArray[0] app.openDocument( document, fileURL, function(doc,wasOpen){ if(doc instanceof Document){ console.log("opened") var db = doc.windows[0].selection.database db.inbox.forEach((tsk)=>{ // task processing goes here }) } else {console.error(doc)} } ) })

(01) • Store a new instance of the FilePicker class in the variable: picker

(02-05) • Set the parameters of the picker instance to allow the selection of a single OmniFocus file.

(06) • Display the file picker dialog to the user. The result of doing so is a JavaScript Promise object that represents the completed result of the picker display.

(08-23) • Call the then(…) function on the stored Promise object to process the chosen file, whose URL is passed into the callback function in an array of URLs containing a single URL item.

(09) • Extract the file reference URL from the passed array of URLs.

(10-22) • Call the openDocument(…) function on the Application object, passing-in the file reference URL. The processing function is passed a reference to the document once it has been opened.

(13-21) • The callback function is passed a reference to the opened document.

(16) • Use the selection property of the Document Window class to derive a reference to the backing database.

(17-19) • Iterate any tasks in the InBox, performing actions on each one.