(Solved) How to modify task note via omnijs and Shortcuts (URL call)

I successfully created my first omnijs script to modify the note of a specific task: it is executed by “open URL” in shortcuts app (see screenshot). The omnijs script as such is working fine. However, after calling omnifocus via URL to perform the change of the selected task note, shortcuts app does not get control again automatically - I have to activate the shortcuts app manually. Only then the script can get finalized and everything is fine.
Some ideas on how to avoid this additional step???

IMG_9F0CBEF1F2ED-1|285x499

Execution of the shortcut continues in the background after the ‘Open URL’ has been triggered. You can verify this by adding a ‘Show Alert’ action: it will display in compact mode on top of the OF window. This behaviour can be avoided with the ‘Wait to return’ action.

In your screenshot there is no further processing?

… yes, the execution of the URL Scheme is the last action in the script. Show alert as well as Wait to return did not solve the problem.
But when I have omnifocus and shortcuts app in split screen view - everything is fine.
When putting the URL Scheme manually in Safari I get the question: “open in Safari” - maybe this is part of the problem.
Maybe, there is also an issue with my omnijs-coding? I thought, I followed the template rightly …

function appendNote(args){
var win = document.windows[0]
var tasks = win.selection.tasks
tasks.forEach(task => {
task.note = args + “\n” + task.note
})
}
var functionInput = "TEST "

var functionString = appendNote.toString()
var encodedFunction = encodeURIComponent(functionString)
var inputString = JSON.stringify(functionInput)
var encodedInput = encodeURIComponent(inputString)

var targetAppName = “OmniFocus”.toLowerCase()
var op = “%28” // open paren
var cp = “%29” // close paren
var urlString = ${targetAppName}://localhost/omnijs-run?script=${op}${encodedFunction}${cp}${op}argument${cp}&arg=${encodedInput}

I’m trying to understand your flow and where the problem lies. Looking at your screenshot, are you getting the target task (the one to modify the note of) in Shortcuts and displaying it with the ‘Show in OmniFocus’ action?

Yes, I did it that way, MultiDim.
Meanwhile I found out how to modify the note of the respective task without displaying it via "Show in Omnifocus. With the adapted omnijs- (see below) and Shortcuts-scripts, meanwhile everything works as expected.
=> issue solved.

function appendNote(args){
var pos = args.indexOf(",")
var id = args.slice(0, pos)
var neu = args.slice(pos + 1)
Task.byIdentifier(id).note = neu + “\n” + Task.byIdentifier(id).note
}
//TEST needs to be substituted by “<task.id>,”"
var functionInput = “TEST”

var functionString = appendNote.toString()
var encodedFunction = encodeURIComponent(functionString)
var inputString = JSON.stringify(functionInput)
var encodedInput = encodeURIComponent(inputString)

var targetAppName = “OmniFocus”.toLowerCase()
var op = “%28” // open paren
var cp = “%29” // close paren
var urlString = ${targetAppName}://localhost/omnijs-run?script=${op}${encodedFunction}${cp}${op}argument${cp}&arg=${encodedInput}

That’s great. Getting the task’s ID in the Shortcut and passing it as the argument to the function is the way I would do it too.

For anyone reading the thread, the Item objects returned by the OF ‘Find Items’ action in Shortcuts have several attributes which include the ‘PrimaryID’, so you can use the ID by modifying the Item magic variable in a subsequent action.