I’ll be grateful for any help. Here’s the script:
-- save this as a "stay open" application with no "startup screen"
-- you must launch the app, not run it from the script editor window.
on do_some_work()
display dialog "I'll do my work now."
-- put more statements here:
end do_some_work
--------------------------------------
on idle
do_some_work() of me
return 2 -- seconds (sets the wait time)
end idle
--------------------------------------
on quit
display dialog "This script should be kept running all the time. If you need to restart, go ahead and quit. The script will be launched automatically at startup." buttons {"Don't Quit", "Quit"} default button 1
if the button returned of the result is "Quit" then
continue quit
else
idle -- this works only once, then nada.
end if
end quit
It works just fine until I try to quit. The quit handler works but the script does not go back to idle.
Thank you,
icta
That’s bad. The ‘on quit’ catch stops the script from quitting, but doesn’t keep the ‘idle’ connection going with the system.
It’s the system that repeatedly calls the script’s ‘idle’ handler. When you use ‘idle’ in the ‘quit’ handler, the idle’s executed just once, like any other handler. You actually need to restart the applet. Applets don’t appear to support the ‘reopen’ command, so the only way I can see to do it is:
on quit
display dialog "This script should be kept running all the time. If you need to restart, go ahead and quit. The script will be launched automatically at startup." buttons {"Don't Quit", "Quit"} default button 1
if the button returned of the result is "Quit" then
continue quit
else
tell application "Finder" to open file (path to me as Unicode text)
end if
end quit
Thank you Nigel, this works quite nicely.
If you have time, try running the following script while watching it with Activity Monitor. It’s the same script as my original, except for the “else” statement in the “on quit” handler.
-- save this as a "stay open" application with no "startup screen"
-- you must launch the app, not run from the script editor window.
on do_some_work()
display dialog "I'll do my work now."
-- put more statements here:
end do_some_work
--------------------------------------
on idle
do_some_work() of me
return 2 -- seconds (sets the wait time)
end idle
--------------------------------------
on quit
display dialog "This script should be kept running all the time. If you need to restart, go ahead and quit. The script will be launched automatically at startup." buttons {"Don't Quit", "Quit"} default button 1
if the button returned of the result is "Quit" then
continue quit
else
-- nothing should be needed here
end if
end quit
On my sytem, the script uses less than 10% of one of the processors while running normally. Then, after typing “Command + Q”, then clicking the “Don’t Quit” button, the processor usage climbs to 100% even though nothing appears to be happening (CPU tab). The activity seems to be pretty normal in all the Disk Activity and Network tabs. What do you think is happening? Stuck in a loop?
Many thanks,
icta
I’m not sure. Could be. The only way to resume normal usage and execution is to give the applet a kick either by clicking its icon in the Dock, double-clicking its file icon, or using the hack I suggested.