Idle Handler Question

This question is in regards to commands within a idle handler. I’m writing a stay-open script to do the following:

  1. Automatically mount the server (after being asked if he/she wants to cancel)
  2. Launch a program
  3. Verifying if the program is active while idle
  4. If the program quits (as per user), the script logs user off the server and then quits itself

Simple right? Well i’m still a newbie at scripting so I need some guidance. This is what I have thus far:

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 tell me to quit
end tell

on idle
	mount volume "afp://username:password@ip address/  OSX Network Applications"
	set thepath to open file "path:application"
	set B to "path:application"
	if B is true then repeat
	end repeat
	if B is false then
		tell application "Finder" to eject disk "  OSX Network Applications" & (quit)
		return 1
	end if
end idle

What I believe is inside the idle handler the open file command cannot be identified or it gives me a “Disk some object” error (?!) . Is there another way to pull this off or should the application launch outside of the idle handler (and how would that be setup)?

Thank you for the insight that anyone can provide.

PS - Thank you both Craig Smith and Adam Bell for the recent tutorial and help (although I need a bit more assistance).

Model: MacBook
AppleScript: 2.1.1 (81)
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

That is not the way to run multiple commands. Also, that will just cause the Finder to quit.

I’d think something like this would work better:

if B is false then
	tell application "Finder" to eject disk "  OSX Network Applications"
	tell application "System Events" to log out
	quit
end if

When you’re testing an on idle, comment out the on idle and end idle lines and run it. It should work as expected. Then apply the on idle. Otherwise, you won’t get any error messages unless you use a try block and display them from within the handler.

Thank you both Adam and Bruce.

I took your advice and made changes as specified. What’s happening now is that the script waits 10 or 18 seconds (based on whether you’ve quit the set to B application or not) quickly ejects the server, re-mounts then launches the application again! (Script Editor after the second launch of the application saying “Disc some object wasn’t found”).

So i’m figuring the set B to “path:application” is not properly identified within the idle handler or telling Systems Events to log out isn’t being fully understood. Actually I had to add a “tell app System Events” on the second line of the idle or it wouldn’t process “set thepath to open” command (Script Editor gave me an error saying the application can’t handle plugins?!).

Should I be using a property function at the beginning of this script for the application I’m trying to set to B? If so what’s the proper syntax?

(the name of the application is “Essential Skills” and the path is “Macintosh HD:Applications:Essential Skills SE:Basic Skills Series”)

B will never be false in your script; You only set it to a string.

Try something like this:

on run
	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 quit me
	
	try
		mount volume "afp://username:password@ip address/  OSX Network Applications"
		open file "path:application"
		-- delay 5 -- If needed
	on error errMsg number errNum
		display alert "Error " & errNum message errMsg
	end try
end run

on idle
	tell application "System Events" to (process "Essential Skills" exists)
	
	if not result then
		try
			tell application "Finder" to eject disk "  OSX Network Applications"
			tell application "System Events" to log out
			quit me
		end try
	end if
	
	return 10 -- Check every 10 seconds. (The default is 30 seconds.)
end idle

I appreciate the reference Bruce.

Unfortunately I’m still having a lot of difficulties with said script. “Quit me” isn’t recognized (Mac-Intel possibly has different versions of AS?) while result returned from the button “Cancel Application” within the run handler negates the “tell me to quit” command (which outside the handler it does not). Also when telling System Events to process exists for Essential Skills it bypasses its existence and continues going through the rest of the idle handler.

When System Events “logs out” it prepares shut down for the machine (not the script) which is a pretty cool trick, but I just want to exit the script (which is typically handled by “tell me to quit”).

I made some adjustments on my end to make the majority of things working (by placing only the display dialog outside the idle handler), only problem is that it repeats the entire script (via return 10) as opposed to just checking whether Essential Skills (the active networked application) exists.

Is there anyway to keep idle a certain branch of the code (do anchors exist)?? Plus how can I make System Events identify Essential Skills?

Hi.

‘tell me to quit’ only affects the script applet’s application mechanism and this only quits when it’s finished doing what it’s doing “ ie. running the script. You need to follow the ‘quit’ command immediately with a “User Canceled.” error, which stops the script execution at that point and allows the applet to quit.

on run
	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
		quit -- Quit when finished.
		error number -128 -- Finish now
	end if
	
	-- Rest of run handler.
end run

Nigel, Bruce and Adam,

THANK YOU!!! :smiley:

Your expertise helped me in accomplishing network based scripts that function perfectly in the lab.

If there’s anything I can assist with (outside of my basic understanding of AppleScript) please don’t hesitate to ask.

-Anthony :cool: