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:

The List actions
  1. List All Projects
  2. 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:

Double-click the “List All Tags” “Script Filter” step to summon its preferences window (shown below):

THe List Tags Script Filter preferences window

 1  Keyword • Enter the keyword, phrase, or character combination you wish to use as this action’s keyword. If you plan on sharing this action with others, enter an Alfred variable {var:listAllTagsKeyword} so that users can set their own keywords during the workflow installation.

 2  Argument Status • Since you want to enable the ability to filter the displayed list, select this checkbox and choose the “Argument Optional” menu option form the menu.

 3  Title and Subtext • Fill in the title and subtext for the action. Be sure to enter a string that appears when the script is taking more than a second or two.

 4  Script Language • JXA (JavaScript for Automation) is listed as using the osascript command line tool.

 5  Enable FIltering • Enable the option for Alfred to filter the results.

 6  The Listing Script • The JXA script code.

Double-click the “List All Tags” “Open URL” step to summon its preferences window: (shown below)

List Tags Open URL preferences

 1  OmniFocus Object Link URL • The passed argument {query} will contain the unique ID of the chosen tag, which is appended to the end of the URL for selecting the OmniFocus Tag in the app interface..

 2  Targeted Application • The target application: OmniFocus

 3  Encoding Options • THere’s no need to encode an OmniFocus identifier as they are already URL-compliant.

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:

List All Tags in Alfred

Here’s the “List All Projects” action:

List All Project action in Alfred