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 two instance properties:
id (ObjectIdentifier r/o) • Returns the identifier object for this object.
url (URL or null r/o) • (4.5+) Returns a URL which links to this database object (tag, task, project, or folder), if one exists.
An example of getting the url of a DatabaseObject, and then using the objecctForURL(…) function of the Database class to derive an object reference to the object targeted by the URL:
Object’s URL
aTask = inbox[0]
if (aTask){
taskURL = aTask.url
//-> [object URL: omnifocus:///task/hX3u3oHdgEm]
objectForURL(taskURL)
//-> [object Task: Plan Party]
}
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:
objectClass (Object or null r/o) • Returns the constructor object that would be used for instances of the class for this ObjectIdentifier.
primaryKey (String r/o) • Returns the unique primary key of the object identifier.
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:
added (Date or null) • Returns the date the object was first saved. If the object is newly inserted, this will be null. For newly inserted objects, the added property may be set (but once an object is saved for the first time, the property is read-only).
modified (Date or null) • Returns the date the object was most recently modified. If the object is newly inserted, this will be null. For newly inserted objects, the modified property may be set (but once an object is saved for the first time, the property is read-only).
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){
cal = Calendar.current
targetDateStart = cal.startOfDay(targetDate)
dc = cal.dateComponentsFromDate(targetDateStart)
dc.day = dc.day + 1
dayAfterTargetDate = cal.dateFromDateComponents(dc)
matches = flattenedTasks.filter(task => {
return (task.added >= targetDateStart && task.added < dayAfterTargetDate)
})
return matches
}
date = new Date("12/13/24")
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:
active (Boolean) • If true, then this object is considered active, otherwise the object is considered dropped.
effectiveActive (Boolean r/o) • Returns true if this object and all its containers are active.
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.2",
"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",
"paletteLabel": "Link-Back Task",
"image": "link.circle"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
currentTask = selection.tasks[0]
// generate link to the selected task
linkURLStr = "omnifocus:///task/" + currentTask.id.primaryKey
taskNames = new Array()
inbox.forEach(task => {
if(task.taskStatus === Task.Status.Available){
taskNames.push(task.name)
}
})
// GENERATE USER INPUT FORM
textInputField = new Form.Field.String(
"textInput",
null,
null
)
inputForm = new Form()
inputForm.addField(textInputField)
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
}
formPrompt = "Enter the title for the new task:"
buttonTitle = "Continue"
formObject = await inputForm.show(formPrompt,buttonTitle)
textValue = formObject.values['textInput']
newTask = new Task(textValue)
newTask.note = linkURLStr
// show the new task
newTaskURLStr = "omnifocus:///task/" + newTask.id.primaryKey
URL.fromString(newTaskURLStr).open()
});
action.validate = function(selection, sender){
return (selection.tasks.length === 1)
};
return action;
})();