• Did you order new AirTags? We've opened a dedicated AirTags forum.

WillGonz

macrumors member
Original poster
Sep 15, 2005
65
15
I got an AppleScript I wrote that is running in a Repeat loop with a delay of two seconds. However, when I save it as an application and then run it won't close when I try to quit it or it won't close on shutdown (I have to force quit). How do I have the application watch for close requests and then close?

repeat
Code:
repeat
    try
     #do a bunch of stuff
on error errTest number errNum
    log {errtext, errNum}
   end try
  delay 2
end repeat
 

tkermit

macrumors 68040
Feb 20, 2004
3,513
2,687
You should replace the repeat loop with an idle handler. It will be called periodically and gives your script the chance to react to quit requests after returning from it. The return value determines the number of seconds that the script waits before automatically calling the handler again. Make sure you save the script as a Stay-Open application (i.e., with the option to "Stay open after run handler" enabled)

idle and quit Handlers for Stay-Open Applications

Code:
on idle
    try
        #do a bunch of stuff
    on error errTest number errNum
        log {errtext, errNum}
    end try
    return 2
end idle
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.