Jump to content

how to pass variables from javascript to subsequent actions


Recommended Posts


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!

CleanShot_2023-06-28_222019@2x.png

Edited by YuTang
Link to comment

I change it a little(as is shown in the following), and now the debug info is: 

 

ERROR: 1-Search OmniFocus JS[Run Script] execution error: Error: ReferenceError: Can't find variable: URL (-2700)

 

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);

url = URL.fromString(url);
url.open();

 

Actually, I'm not that familiar with coding, so do need your help!

Edited by YuTang
Link to comment

I haven't used JXA yet, but I see two possible issues given the error message.

 

const url = "bluebird://add?title=" + encodeURIComponent(taskName) + "&notes=" + encodeURIComponent(taskNote);

 

Declaring `url` as const makes it immutable, non? On the next line, however, you're trying to change the variable. 

Or the global `URL` object is not found and perhaps has to be imported individually.

URL.fromString(url);

 

if you are still using bash, you should be able to drop the last two lines and open the url like this:

 

open "$url"

 

(or print `url` and feed it into another Run Script instance where you open it with bash)

Link to comment

I think the problem might be 

const _task = app.Task({id: theQuery, in: defaultDoc});

does not actually fetch the proper task with the given id. So I wonder if there is any way to get the metadata of OmniFocus tasks needed via their id. Do need your help🥹.

Link to comment

Thank you for your reply! Finally I solve it myself, now I use a simple AppleScript.
 

tell application "OmniFocus"
	set theQuery to "atlviq9dkBl"
	tell default document
		set theTask to task id theQuery
		set taskName to name of theTask
		set taskNote to note of theTask
		set encodedTaskName to my urlEncode(taskName)
		set encodedTaskNote to my urlEncode(taskNote)
		
	end tell
end tell


set resultText to encodedTaskName


on urlEncode(str)
	local str
	try
		return (do shell script "/bin/echo " & quoted form of str & ¬
			" | perl -MURI::Escape -lne 'print uri_escape($_)'")
	on error eMsg number eNum
		error "Can't urlEncode: " & eMsg number eNum
	end try
end urlEncode

set bluebirdLink to "bluebird://add?title=" & encodedTaskName & "&notes=" & encodedTaskNote
open location bluebirdLink

 

Actually, I tried an AppleScript like this at the very beginning, yet I name the variable bluebirdLink as url, which strangely cause an error: Can’t set URL to "bluebird://add?title=" & encodedTaskName & "&notes=" & encodedTaskNote. Access not allowed.
after I changed it, it's now ok. Thank you again!~!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...