Interoperability: Omni Automation and Alfred
DISCLAIMER: Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. OMNI-AUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. OMNI-AUTOMATION.COM provides this only as a convenience to our users. OMNI-AUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and OMNI-AUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from OMNI-AUTOMATION.COM and that OMNI-AUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.
Listing Actions
This section concerns two Alfred actions that generate lists of OmniFocus objects:
- List All Projects
- List All Tags
Both actions are powered by scripts written using JXA (JavaScript for Automation) a version of JavaScript shipping in macOS that executes Apple Events, in a manner similar to AppleScript.
These JXA scripts, included in the Alfred actions, provide a mechanism to access specific OmniFocus Projects or Tags very quickly from anywhere in macOS.
Returning Data to Alfred
Both scripts gather information about the targeted OmniFocus items and returns the data to the Alfred action to be used in the display of results. The data is returned as JSON (JavaScript Object Notation) looking similar to the following:
Response JSON
{
"items":[
{
"title": "Tag Title",
"subtitle": "4 tasks remaining",
"arg": "dvpyM40skEI"
}
]
}
NOTE: The highlighted object keys (line 12-14 below) items title subtitle arg are some the available JSON keys supported by Alfred’s Script Filter step which is used in the custom action to parse and display results.
The corresponding values for the keys are generated by the script dynamically for each matched item: item name (title), status information (subtitle), and the items’s unique ID string (arg) which will be used by the Alfred “Open URL” step to construct a link to the matched items in OmniFocus.
The generated JSON object is returned by the script to Alfred to be further processed and displayed.
List All Projects
// GET ALL PROJECTS
const allProjects =
Application("OmniFocus").defaultDocument.flattenedProjects()
// EXTRACT PROJECT DATA TO SCRIPT FILTER ITEMS
const sfItems = allProjects.map(project => {
const projName = project.name()
const projID = project.id()
const taskCount = project.numberOfTasks()
const remainTaskCount = taskCount - project.numberOfCompletedTasks()
return {
title: projName,
subtitle: `${remainTaskCount} tasks remaining of ${taskCount}`,
arg: projID
}
})
// OUTPUT JSON
JSON.stringify({items:sfItems})
List All Tags
// GET ALL TAGS
const allTags = Application("OmniFocus").defaultDocument.flattenedTags()
// EXTRACT TAGS DATA TO SCRIPT FILTER ITEMS
const sfItems = allTags.map(tag => {
const tagName = tag.name()
const tagID = tag.id()
const remainTaskCount = tag.remainingTaskCount()
return {
title: tagName,
subtitle: `${remainTaskCount} tasks remaining`,
arg: tagID
}
})
// OUTPUT JSON
JSON.stringify({items:sfItems})
Building the Actions
Each listing action is comprised of two steps connected together:
- Script Filter
- Open URL
Double-click the “List All Tags” “Script Filter” step to summon its preferences window (shown below):
Double-click the “List All Tags” “Open URL” step to summon its preferences window: (shown below)
Running the Actions
To run the “List All Tags” action, summon Alfred and enter your chosen keyword (default is tgs)
Type a space and characters to filter the returned list.
Select a Tag and press the return key to have the chosen Tag selected in OmniFocus for you:
Here’s the “List All Projects” action: