Scriptable not returning result to Shortcuts

I have a Shortcut that gets current weather details, passes the humidity and dewpoint into a Scriptable script, which then performs some calculations and sends a text string back.

The relevant shortcut steps:

  1. Runs script shortScriptRouter with shortcutParameter “weather”, and 2 other number inputs passed in as “Texts”. Run in App is selected.
  2. Wait to return (I’ve had some issues with async functions not being waited for, but the issue remains if I remove this step).
  3. Use result of script in a text step. I’ve tried passing the result back as both dictionary and text with no result.

In Scriptable:

  • shortScriptRouter takes input and passes it to the appropriate function.
  • Function is called and I’ve confirmed that it returns the correct result.
  • shortScriptRouter imports a util called getScriptIO – this takes the Scriptable args object as input and uses it to determine whether the script was called via xcallback, Shortcuts, or neither. It returns the call type, the relevant script input (e.g. shortcut parameters), and a function returnResults.
  • For Shortcuts, the returnResults function looks like this:
payload => {
  if (payload) Script.setShortcutOutput(payload);
  Script.complete();
  url("shortcuts://").open();
}

I’ve also confirmed that returnResults does have the correct payload, so the issue seems to come from calling the Script methods somehow.

When running the shortcut, quick peeking the return result shows that there is nothing passed back.

Not sure exactly what’s going on :man_shrugging:

I should note that this all works fine when I don’t select “Run in App” in Shortcuts – but I’ve found that if I don’t run it in Scriptable, I often run into communication errors, I guess memory related?

(PS – I’m debugging the scripts with notifications, as someone else has mentioned elsewhere, would be nice to have the openEditor parameter available for the Shortcut action)

I’m new to Scriptable and Shortcuts, so my input may not be accurate at all.

Just by looking at your description, I suspect Script object in your getScriptIO module might be pointing to getScriptIO script itself; rather than shortScriptRouter script that you want.
Since you are running shortScriptRouter, I wonder if Script.setShortcutOutput() /* as in getScriptIO.setShortcutOutput() */ set the output to the correct pointer.

Try to print Script.name() in your getScriptIO.returnResults function.

Btw, what async issue did you have? I’m facing a timeout failure when I run the script via Run Script action in Shortcuts; it works well when I run it via x-callback-url. Any idea?

1 Like

Just tried the Script.name() inside the returnResults fn, but it showed shortScriptRouter. I think you’re probably on to something though, I think there probably are some issues in the way I’m accessing Script, I’ll try some other things and post here if I find a solution.

Btw, what async issue did you have? I’m facing a timeout failure when I run the script via Run Script action in Shortcuts; it works well when I run it via x-callback-url. Any idea?

Yeah it’s a bit sensitive. The issue I see is that Scriptable doesn’t seem to be awaiting when I ask it to when calling the Script from Shortcuts.

The 2 last lines of the script are setShortcutOutput and Script.complete(). These should only be called after the async function completes, but it’s clear that it’s returning to Shortcuts before the function has finished. Sorry, I haven’t found any solution to this so far.

Script.setShortcutOutput() only works when “Run in App” is switched off. I don’t know why, but it is true.
Unless your Scriptable script needs to access folders in iCloud, Switch off “Run in App” in the Shortcut parameters. Then the Scriptable script will work in the background (you will not see Scriptable running at all) and you’ll get the results in Shortcuts.

Aha that may be the core of the issue :smiley: Unfortunately I’ve run into too many issues with trying to run scripts w/o “run in app” (I’m guessing either memory or iCloud sync), so I’ll just have to stick with using xcallback in these cases to return values