Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

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
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.