Script.complete() seems to not kill the script when run in app

Using the code below, you can see that the script execution is expected to stop before the preview of the widget is shown. This seems fine when used in a widget, but running in app, the preview still shows.

I’ve known this to operate this way for as long as I can remember, but I’m wondering if it is intended.

//set the filename of the image. Example: image in iCloud Drive/Scriptable which has the name "WBack.jpeg"
let filename = "WBack.jpeg"

//setup the fiie manager
let fm = FileManager.iCloud()

//build the full path of the image
let path = fm.joinPath(fm.documentsDirectory(),filename)

log(`path is ${path}`)

//retrieve the image
let image = fm.readImage(path)

//start to build the widget
let w = new ListWidget()
//set background image as the image variable
w.backgroundImage = image
//set the widget
Script.setWidget(w)
//complete the script
Script.complete()
//display a preview when running in app
w.presentLarge()

From the documentation I deduce that it only has an effect, when it is not run in the app (with Siri, in Shortcuts, as a share sheet extension, in a widget, in a notification,…). Apart from that, it would require Scriptable to kill the process where the code is run. Simply throwing an exception wouldn’t be enough since you can easily catch that.
Somewhere (I don’t remember anymore) I read that Simon (the developer) hadn’t implemented any logic for killing script instances in the context of a stop button for scripts if they continued indefinitely. Though this information is a few app versions old and it seems that there is kind of a stop, when you exit the script editor.

tl;dr: It would need the app to kill the script instance and I read that this isn’t implemented (for a stop button).

1 Like

I thought the same. But the part of the documentation made me double guess myself:

If you find that a script takes too long to complete, you can manually call the complete function to stop the execution. Note that this should be done as the very last action the script performs.

This made me think that calling Script.complete should stop execution but maybe that’s only when running it from another script?

I also was just informed in the r/Scriptable Discord server that Script.setWidget isn’t necessary for a widget to be displayed on the Home Screen or in app if you are already using a ListWidget.present{size} item.

Was this intentional implementation?

If I wanted to stop exit from the script for some reason, I just call return. The whole script itself is executed inside an internal async function _scriptable_run().

1 Like

Oh yes that’s true. I forgot about that.

Still wondering if it was intended. I’ve always believed it was but when I read the documentation I started second guessing myself