Resolved AppleScript - Hangs shutdown process

Discussion in 'Mac Programming' started by WillGonz, Jun 28, 2016.

  1. WillGonz macrumors member

    Joined:
    Sep 15, 2005
    #1
    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
     
  2. tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #2
    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
     
  3. WillGonz thread starter macrumors member

    Joined:
    Sep 15, 2005

Share This Page