How to use OmniFocus task variables in Alfred workflow

I made some minor adjustments to the script based on rhydlewis/alfred-search-omnifocus: An Alfred workflow that runs free text searches on OmniFocus data (github.com) to fit my own workflow. I want to use this script to retrieve metadata of the selected task(such as task name, project name, due date, notes, tags, etc.) from the list of matching results and then to set a timer in a pomodoro app called Bluebird using these metadata.

Initially, my script looked like this:

const app = Application('OmniFocus');
app.includeStandardAdditions = true;

// Get metadata of the selected task
const defaultDoc = app.defaultDocument;
const theQuery = '{query}';
const _task = app.Task({id: theQuery, in: defaultDoc});
const taskName = _task.name;
const taskNote = _task.note;

// Add task in a pomodoro app called Bluebird and begin working session
const url = "bluebird://add?title=" + encodeURIComponent(taskName) + "&notes=" + encodeURIComponent(taskNote);

app.openLocation(url);

However, I quickly found out that OmniFocus cannot open URL schemes that don’t start with “omnifocus.” Therefore, I came up with a plan B: I can just try to pass the variables $taskName and $taskNote from the script and append an Alfred action to open the URL: bluebird://add?title={var:taskName}&notes={var:taskNote}. However, since I’m not familiar enough with Alfred, I don’t know how to adjust the script to pass the variables.

Here is the adjusted script based on the suggestion from ChatGPT:

# Get task name and task note from OmniFocus using JXA (JavaScript for Automation)
taskName=$(osascript -l JavaScript -e '
var app = Application("OmniFocus");
app.includeStandardAdditions = true;
var defaultDoc = app.defaultDocument;
var theQuery = "{query}";
var _task = app.Task({id: theQuery, in: defaultDoc});
var taskName = _task.name;
taskName;
')

taskNote=$(osascript -l JavaScript -e '
var app = Application("OmniFocus");
app.includeStandardAdditions = true;
var defaultDoc = app.defaultDocument;
var theQuery = "{query}";
var _task = app.Task({id: theQuery, in: defaultDoc});
var taskNote = _task.note;
taskNote;
')

# Store these values in Alfred's workflow environment variables
echo "{ \"alfredworkflow\" : { \"variables\" : { \"taskName\" : \"$taskName\", \"taskNote\" : \"$taskNote\" } } }"

Sadly, it just did not work. It would be highly highly appreciated if you could please help me out!

1 Like

Greetings. We recently posted a section on the Omni Automation website for interoperability with Alfred. Perhaps this documentation can provide some insight in answering your query?

2 Likes