I have a small dilemma regarding an stay-open (idle) script I’m writing for network based applications here at the lab. Basically what my script is allowing is children to log into the server, launching the application and the script staying resident until the child quits the program (where my script takes over and logs them off the server as well). Everything worked flawlessly with the exception of one thing - the stay-open script cannot stay open! Everything worked great until I added an addendum allowing the user to cancel the application before the script kicks in. Here’s what I have so far:
on run
tell application "Finder"
display dialog "Launching Essential Skills...have fun!" buttons {"Cancel Application"} default button 1 giving up after 3
if button returned of result is "Cancel Application" then return
mount volume "afp://user:password@ip address/ OSX Network Applications"
set thepath to open file "Macintosh HD:Applications:Education Applications:Essential Skills SE:Basic Skills Series.app"
end tell
end run
on idle
tell application "Finder" to set exists_ to exists process "Essential Skills SE:Basic Skills Series"
repeat until exists_ is false
end repeat
if exists_ is false then
tell me to quit
else
tell application "Finder" to eject disk " OSX Network Applications"
end if
return 1
end idle
I’m sure there’s different ways of pulling this off and again, without the variable of prompting the user to cancel before the script fully kicks in it worked great. All I ask is efficient code that doesn’t have much CPU overhead while running continuously until the app (Essential Skills) is finished or the user cancels it altogether.
Thanks for your input.
Model: MacBook
AppleScript: 2.1.1 (81)
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
on run
(*do setup - this runs when the application is started to set things up. It is not necessary to have an 'on run' handler because the on idle handler runs immediately after this anyway. If you want this to run all the time after every login, list it your startup items. You quit it from it's dock icon.*)
end run
(*'on idle' runs immediately following the 'run' handler if there is one, and thereafter on the interval specified in the return at the end. (An 'on run' handler is not required, so omit if there is no setup)*)
on idle
-- In this section, you do your script operations every interval
return xx -- do this every xx *seconds*.
end idle
on quit
(*do stuff - assumes you want to clean things up or save preferences or change properties before the program quits. This section runs only if the application is quit from the dock, or by any other means. It is not necessary to have one.*)
(*if there is an on quit handler, then 'continue quit' must be the last statement or the script will not stop! The presence of an 'on quit' handler 'grabs' the system command to stop, so it will not complete the quit process until notified to do so. 'continue quit' does that.*)
continue quit
end quit
Your return quits the application before it gets to the on idle handler. I assume you want this to be a one-time thing, but until you’re clearer about your objectives, can’t suggest much more.
Thank you Adam for the quick reply - I appreciate the insight on idle handlers. I’ve eliminated the run handler as you suggested.
Maybe I should have been a bit clearer on my intentions of the program. Basically my goals are to have the user decide after launching the script to:
A) Decide within the first three seconds to cancel the execution of the script (through a text box) and kill the “stay open” status
B) (IF NOT, THEN) Log into a local server and launch the program (Essential Skills)
C) Keep the script running in the background while checking every few seconds to see if the program (Essential Skills) is still running.
D) When Essential Skills is terminated to log off the server.
Pretty simple stuff. As I mentioned earlier the script ran great but it was the inclusion of the cancel option at the beginning that made things difficult. What happens now is that everytime I engage the cancel function in the stay open script it prompts me that the " OSX Network Applications" volume isn’t mounted. I mean I can cut corners here and eliminate the cancel text box but I’d really like to know WHY this happening and how I can prevent it.
I apologize for my newbie level of scripting - I only started this a few months back.
So can anyone assist me on this script? The only changes that were made to the script above were removing the “on run” handlers. My problem with the script is that it prematurely closes after launching the Essential Skills program and mounting the server. If I rearrange the eject/quit commands within the idle handler it then gives me the error of saying the server isn’t mounted.
Is there a way for the script to verify that it is or is not mounted and based on that keep it’s “stay open” status?
Thank you.
FYI:
A) Decide within the first three seconds to cancel the execution of the script (through a text box) and kill the “stay open” status - WORKING
B) (IF NOT, THEN) Log into a local server and launch the program (Essential Skills) - WORKING
C) Keep the script running in the background while checking every few seconds to see if the program (Essential Skills) is still running. - NOT WORKING
D) When Essential Skills is terminated to log off the server. - NOT WORKING (originally this worked fine but since the script quits early…)