YuTang Posted June 28, 2023 Share Posted June 28, 2023 (edited) 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) + "¬es=" + 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}¬es={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! Edited June 28, 2023 by YuTang Link to comment
YuTang Posted June 28, 2023 Author Share Posted June 28, 2023 ps: I have already adjusted the language from /usr/bin/osascript to /bin/bash! Link to comment
YuTang Posted June 29, 2023 Author Share Posted June 29, 2023 (edited) 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) + "¬es=" + encodeURIComponent(taskNote); url = URL.fromString(url); url.open(); Actually, I'm not that familiar with coding, so do need your help! Edited June 29, 2023 by YuTang Link to comment
zeitlings Posted June 29, 2023 Share Posted June 29, 2023 I haven't used JXA yet, but I see two possible issues given the error message. const url = "bluebird://add?title=" + encodeURIComponent(taskName) + "¬es=" + 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
YuTang Posted June 30, 2023 Author Share Posted June 30, 2023 Thank you for your replied, I've adjusted them, yet it still doesn't work. Link to comment
YuTang Posted July 1, 2023 Author Share Posted July 1, 2023 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
zeitlings Posted July 1, 2023 Share Posted July 1, 2023 I don't use Omnifocus personally, so I can't offer much help myself. Have you tried looking for inspiration from other users who may have faced similar challenges on the Omnigroup forums? You can also ask for help there, as they should be best equipped to provide you with answers on how to use their software. Link to comment
YuTang Posted July 1, 2023 Author Share Posted July 1, 2023 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 & "¬es=" & 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 & "¬es=" & encodedTaskNote. Access not allowed. after I changed it, it's now ok. Thank you again!~! zeitlings 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now