idle handler not always shutting down applescript.

Hi All
I have set an applescript set to run using a launch agent on login.
The script opens ichat/messages and keeps it open even if the user quits, however some users are struggling to shutdown their macs due to the time/amount of applications to shut etc in the time period in the script.

Is there a way to quit the script when shutdown has been selected?

on idle
	-- Starts or quits depending on connection
	set theResult to ""
	try
		set theResult to (do shell script "ping -o -t 2 [url=http://www.google.co.uk]www.google.co.uk[/url]")
	end try
	if (theResult contains "1 packets received") = false then
		quit application "Messages"
	else
		try
			tell application "Messages" to launch
			
			-- Changes status of account
			try
				tell application "Messages"
					try
						if status is offline then
							set status of service "Jabber" to available
						else
							if status is away then
								set status of service "Jabber" to away
							else
								if status is available then
									set status of service "Jabber" to available
								else
									if status is invisible then
										set status of service "Jabber" to available
									end if
								end if
							end if
						end if
					on error
						error number -128
					end try
				end tell
			end try
		end try
	end if
	return 15
end idle



Thanks any help appreciated… (thought i had better mention the application is hidden so the user cant quit it.)

Is launching scripts via LaunchAgent something new in Lion/MountainLion?

At least to stop a process launched with launchd (“LaunchAgent”) under Snow Leopard, you’d have to use launchd to stop it because if the process quits, then launcd will re-launch it.

If it is so, then it isn’t like you’ll ever manage to quit that process within itself, then you’d have it call another process that calls LaunhAgent, and makes it quit the first process. When that has happened, then you can have your process quit, and reckon it stays quit. Since I’m not on Lion/Mountain Lion, I don’t know if this is the case.

I’d rather read the documentaion of LaunchAgent at apple.com, which I’m sure there is.

Hi McUsrII

Thanks for the info…the launch agent just starts the script saved as an app on user login on 10.6-10.8.
The script is saved as a an application with the stay open after run handler.

If the script/application was visible to the user the user can quit it. (which they can still do in activity monitor)

But for some reason they have trouble shutting down sometimes with the script starting up ichat before the mac has had a chance to shut down in time.

So i wondered if there was a way of identifying the user had selected shutdown or restart and be able to kill the application.

Thanks

Hello.

Try adding a on runn handler, with an error handler trapping error number −128

on run
	try
	on error e number -128
		tell me to quit
	end try
end run

Also have the same test in each of your try blocks in your error handler, I can’t see anything else, than that that should work. (I think your problem is that the error trapped in your idle handler will activate the run handler, and that the script will “hang” there.)