×

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.

This section concerns the second automation scenario listed on the previous section:

The following documentation details the construction of the second Alfred action in the OmniFocus Collection Alfred Workflow. This action is used to generate a new sequential project in OmniFocus whose name is provided by the user an input in the Alfred interface.

Scenario 2: Passing Data into an Omni Automation Script

Alfred has the ability to pass data into a workflow that is triggered using a keyword. This passed data can be used by an Omni Automation Script URL when it is executed.

In the following example, a new OmniFocus project will be created using the title entered in the Alfred input interface.

To have the Omni Automation script be able to receive the passed data and be “security approvable” for repeated use, it will need to be written as a self-invoking function that receives a function “argument.”

The Script Function

As an example, here is a function that will create and select a new sequential project. This function is self-invoking and will process the text automatically passed as input data through the special argument enumeration (below: line 7).

New Sequential Project


(function newSequentialProject(arg){ project = new Project(arg) project.sequential = true projectID = project.id.primaryKey url = "omnifocus:///task/" + projectID URL.fromString(url).open() })(argument)

Using the Script URL encoding tools provided on this website, here is the encoded function as a Script URL containing the argument enumeration, and the appended &arg= URL parameter with a value of {query} which is the default variable used by Alfred to represent the user-input passed to the function.

NOTE: The &arg= parameter value variable {query} is enclosed in quote marks (" ") because its contained value will be text, which is always placed within quote marks, and that the enclosing quote marks also have been encoded using the percent code: %22

Script URL with Appended Argument Value


omnifocus://localhost/omnijs-run?script=%28function%20newSequentialProject%28arg%29%7B%0A%09project%20%3D%20new%20Project%28arg%29%0A%09project%2Esequential%20%3D%20true%0A%09projectID%20%3D%20project%2Eid%2EprimaryKey%0A%09url%20%3D%20%22omnifocus%3A%2F%2F%2Ftask%2F%22%20%2B%20projectID%0A%09URL%2EfromString%28url%29%2Eopen%28%29%0A%7D%29%28argument%29&arg=%22{query}%22

During the Alfred workflow execution, the variable {query} will automatically be replaced with the encoded text (project title) provided by the user, before the URL is sent by Alfred to OmniFocus.

Creating the Second Alfred Action

The second Alfred action in the workflow is comprised of two steps: (see below)

 A  A Keyword Trigger whose settings indicate the word, characters, or phrase that when entered into the Alfred interface will summon the next step in the action. The default keyword is the letter combination: sq (stands for sequential) You may have previously changed the keyword during the import of the Alfred workflow.

 B  An Open URL step that will execute the provided URL, or in this case an encoded Omni Automation script URL, using a specified application.

The second Alfred action in the workflow

Double-click the Keyword step  A  to summon its preferences window: (see below)

The preferences window for the Keyword step of the action

 1  Keyword • The keyword, phrase, or characters used to summon the Alfred action. NOTE: the keyword may also be represented with an Alfred variable: {var:variableName}

 2  Enable Argument Parameters • Enable specified parameters for the user-provided input.

 3  Require Input • Specify the condition for the user-provided input.

 4  Action Title • The action title that is displayed in the Alfred interface.

 5  Action Description • The action description or instructions displayed in the Alfred interface.

Double-click the Open URL step  B  to summon its preferences window: (see below)

The preferences for the Open URL step

 1  Script URL • Copy and paste the encoded Omni Automation function with the appended &arg= URL parameter and its value into the URL field.

 2  Input Variable • Note that the Alfred {query} placeholder reamins part of the Script URL. Prior to the running the URL it will be replaced with the user-input text.

 3  Target Application • The application that is the target of the script URL.

 4  Encode Placeholder Variable • Select this option to have Alfred percent encode the user-input prior to replacing the variable with the encoded input text.

Executing the Script URL

From within any application, summon the Alfred input and enter the keyword “sq” in the input field. Type a space and then enter the title for the new project:

Alfred Input Field

Type a Return and the Script URL will be executed, summoning the security window in OmniFocus on the first run:

Script Security window showing function

Select the checkbox to enable the script to run subsequently without re-approval.

A new OmniFocus project will be created and selected:

The new OmniFocus project

Next Example

In the NEXT EXAMPLE, Alfred is used to launch an Omni Automation script that requires no user input within Alfred in order to perform a procedure in OmniFocus that returns a list of linked objects to Alfred for display.