I would approach it by iterating using a one-off timer with a 0ms delay.

Inbetween the current script finishing and the new one starting it should execute the event loop and process outstanding events like quit.