如何判断应用程序退出事件

how to determine the application exit event

我使用 applescript 通过脚本编辑器创建了一个应用程序。在代码中,我使用 "repeat ... end repeat" 来模拟一个计时器。编译代码时,我 运行 应用程序。它工作正常,但是,如果我右键单击应用程序图标,然后选择菜单 "Quit",则没有任何反应。 我知道我应该做一个 "EXIT" 检查,但是,我不知道该怎么做。 有人可以帮忙吗?

on main()

    set f1 to "/System/Library/Sounds/Pop.aiff"
    set f2 to "/System/Library/Sounds/Ping.aiff"
    set f3 to "/System/Library/Sounds/Glass.aiff"
    set f4 to "/System/Library/Sounds/Tink.aiff"

    repeat
        set now to current date
        set h to hours of now
        set m to minutes of now
        set s to seconds of now

        if (h = 6) and (m = 30) and (s = 0) then 
                --some code here
        end if

        delay 0.5

    end repeat

end main

重复循环阻塞了代码。

由于代码保存为应用程序,因此使用空闲处理程序,该处理程序根据 return 值定期调用。

global f1, f2, f3, f4

on run
    set f1 to "/System/Library/Sounds/Pop.aiff"
    set f2 to "/System/Library/Sounds/Ping.aiff"
    set f3 to "/System/Library/Sounds/Glass.aiff"
    set f4 to "/System/Library/Sounds/Tink.aiff"
end run

on idle   
    set {hours:h, minutes:m, seconds:s} to current date
    if (h = 6) and (m = 30) and (s = 0) then 
            --some code here
    end if
    return 1
end idle