Orange Peel Orange Peel - 4 months ago 33
AppleScript Question

Exit infinite loop by quitting application

I have an AppleScript application in which I have a background task running in an infinite loop.

repeat while true
-- do some tasks
delay 0.5
end repeat


When I export and run the application, I am not able to quit it normally, instead I have to use the force quit. How am I able to fix this?

Answer

Instead of the infinite loop implement the on idle handler. It allows to consider the quit command.

on idle
    -- do some tasks
    return 1
end idle

However there is a restriction. The minimum interval is one second.