×

The DatabaseObject Class

The DatabaseObject class is a generalized class containing the various elements used by the database to order and store your data, including: folders, projects, tasks, and tags.

These database objects share a common id property that is most often used in conjuction with the built-in OmniFocus URL support to reveal one or more DatebaseObjects in the main window.

Instance Properties

The DatabaseObject class has one property:

The ObjectIdentifier Class

An instance of the ObjectIdentifier class is returned as the value of the id property of the DatabaseObject class. An instance of the ObjectIdentifier class has two properties:

You can incorporate the id and primaryKey properties with the built-in OmniFocus URL support to reveal one or more DatebaseObjects in the main window:

Reveal Named Tag


tag = tagNamed("West") if(tag){ urlStr = "omnifocus:///tag/" + tag.id.primaryKey URL.fromString(urlStr).open() }
Reveal Named Folder


folder = folderNamed("Fall") if(folder){ urlStr = "omnifocus:///folder/" + folder.id.primaryKey URL.fromString(urlStr).open() }
Reveal Named Project


project = projectNamed("My Project") if(project){ urlStr = "omnifocus:///task/" + project.id.primaryKey URL.fromString(urlStr).open() }

To reveal more than one database object, append a comma-delimited list of primaryKeys to the calling URL:

Reveal Matching Inbox Tasks


var keys = inbox.filter(task => { return task.name.includes(" Car") }) if (keys.length > 0){ urlStr = "omnifocus:///task/" + keys.join(",") URL.fromString(urlStr).open() }
Reveal Tasks with Specified Name


taskName = "Drive Car" matches = flattenedTasks.filter(item => { return item.name.toUpperCase() === taskName.toUpperCase() }) if (matches.length > 0){ keys = matches.map(task => {return task.id.primaryKey}) urlStr = "omnifocus:///task/" + keys.join(",") URL.fromString(urlStr).open() }

DatabaseObject: DatedObject

The DatedObject class (a sub-class of the DatabaseObject class) contains elements that track date values referencing the element’s creation and modification. These properties apply to tasks, tags, folders, and projects (for projects, the root task of the project has the dates for the project).

Instance Properties

The DatedObject class has two properties:

DatedObject (Project) Properties


project = projectNamed("My Project") if (project){ console.log(project.task.added) console.log(project.task.modified) }

An example script for creating a new project whose creation date is midnight of the current date:

Set Creation Date of New Project


project = new Project("NEW PROJECT") project.task.added = new Date(new Date().setHours(0,0,0,0))

An example function that uses the added property to gather an array of object references to all tasks added on a specified day:

Tasks Added on Specified Date


function tasksAddedOnTargetDate(targetDate){ var cal = Calendar.current var targetDateStart = cal.startOfDay(targetDate) var dc = cal.dateComponentsFromDate(targetDateStart) dc.day = dc.day + 1 var dayAfterTargetDate = cal.dateFromDateComponents(dc) var matches = flattenedTasks.filter(task => { return (task.added >= targetDateStart && task.added < dayAfterTargetDate) }) return matches } var date = new Date("12/13/20") tasksAddedOnTargetDate(date)

DatedObject: ActiveObject

The ActiveObject class (a sub-class of the DatedObject class) contains elements that are active.

Instance Properties

The ActiveObject class has two properties:

Dropping a Project


var project = projectNamed("My Project") if (project){ project.task.active = false }
Tag Status


var tag = flattenedTags.byName("Camera-Ready") if(tag){tag.active = true}

Link-Back Task

Here’s a plug-in that uses the id and primaryKey properties of the DatabaseObject class to create a new task whose notes field contains a link to the currently selected task.

New Link-Back Task
 

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.of.link-back-task", "version": "1.1", "description": "Creates a new task in the Inbox with a link to the selected task placed in the notes of the new task.", "label": "New Link-Back Task", "shortLabel": "Link-Back Task" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code var currentTask = selection.tasks[0] // generate link to the selected task var linkURLStr = "omnifocus:///task/" + currentTask.id.primaryKey var taskNames = new Array() inbox.forEach(task => { if(task.taskStatus === Task.Status.Available){ taskNames.push(task.name) } }) // GENERATE USER INPUT FORM var textInputField = new Form.Field.String( "textInput", null, null ) var inputForm = new Form() inputForm.addField(textInputField) var formPrompt = "Enter the title for the new task:" var buttonTitle = "Continue" formPromise = inputForm.show(formPrompt,buttonTitle) inputForm.validate = function(formObject){ inputText = formObject.values['textInput'] if (!inputText){return false} if (taskNames.includes(inputText)){ throw "ERROR: a task with that name is already in the Inbox." } return true } formPromise.then(function(formObject){ var textValue = formObject.values['textInput'] var newTask = new Task(textValue) newTask.note = linkURLStr // show the new task newTaskURLStr = "omnifocus:///task/" + newTask.id.primaryKey URL.fromString(newTaskURLStr).open() }) formPromise.catch(function(err){ console.error("form cancelled", err.message) }) }); action.validate = function(selection, sender){ // validation code return (selection.tasks.length === 1) }; return action; })();