Applescript won't complete when idle

Hey folks, I have a strange problem. Im an administrator for a company and set an applescript to run at night on everyones computer. Theyre all using 10.4.11. Some intel, some PPC. The problem is the script works perfectly if I run it manually, but if it triggers on its own at night after the machine has been idle, I find this message: “AppleEvent timed out.” The energy saver settings are set to never sleep, and for the hard drives to not spin down. Heres the script…anyone have any ideas? Thanks in advance…


set countdown to 300
repeat countdown times
	tell me
		activate
		display dialog "YOUR COMPUTER WILL BE SHUTDOWN IN " & countdown & " SECONDS UNLESS YOU CLICK DO NOT SHUT DOWN!" giving up after 1 buttons {"DO NOT SHUT DOWN!", "OK, Please Shut Down now"}
		if button returned of result is "DO NOT SHUT DOWN!" then
			error number -128
		else if button returned of result is "OK, Please Shut Down now" then
			exit repeat
		end if
		set countdown to countdown - 1
	end tell
end repeat

tell application "System Events" to shut down

Hi,

the only line, which can cause the timeout is the “System Events” line.
I guess, there are some open documents left, and to shut down the computer via System Events asks the user to close the documents.
If nobody responses within 2 minutes, the error occurs.
Using the shell command shutdown avoids this problem.


set countdown to 300
repeat countdown times
	tell me
		activate
		display dialog "YOUR COMPUTER WILL BE SHUTDOWN IN " & countdown & " SECONDS UNLESS YOU CLICK DO NOT SHUT DOWN!" giving up after 1 buttons {"DO NOT SHUT DOWN!", "OK, Please Shut Down now"} cancel button "DO NOT SHUT DOWN!"
		if button returned of result is "OK, Please Shut Down now" then exit repeat
		set countdown to countdown - 1
	end tell
end repeat

do shell script "shutdown -h now" password "¢¢¢¢¢¢" with administrator privileges

Note: If you specify the Cancel button, you can omit the error -128 part

Thanks for the reply. Unfortunately I can’t use the shutdown shell script command because it would force quit apps, and lose unsaved potentially important documents. Also, the script never executes the shut down command because when I get to the screen of a machine that was on with unsaved docs open, there are no “Save, Don’t Save, Cancel” dialogs indicating the system attempted to shut down. So the time out is either in asking the System Events to perform the shut down in the first place, or at some point before that. Is it that the idle status is somehow blocking System Events from receiving the command in the first place?

If I execute the same command on an idle machine using Apple Remote Desktop “Send Unix Command” feature:
osascript
tell application “System Events”
shut down
end tell

…it executes perfectly

Ive googled around and there are on idle handler scripts out there designed specifically to execute System Events commands when the system is idle. This leads me to believe theres no inherent reason why this command should fail based on the computer being idle, but I would love a more educated opinion on this.

Alternately, if I can’t use a script in this way, can anyone think of other solutions? Any other ways to perform a graceful shutdown that won’t force quit apps with unsaved docs? Or maybe the answer is to somehow “wake” the machine from it’s idle status? …any ideas?

Many thanks for any help.

This is a paradox.

Either you shut down regardless of open docs or someone has to make the decision to save the docs or dismiss them :wink:

I want the shut down to stop and prompt the user to save if there are open docs…exactly the way System Events shut down works. I used slightly more open wording because if there are clever ways to detect unsaved docs and simply cancel the shut down altogether, that would be suitable.

Im actually in need of a solution to this. Is there anyone that can help?

StefanK has made the salient point, but (I think) if you were to bracket your script with a timeout routine of effectively ‘until tomorrow’, the individual machine would (presumably) not shut down until the application specific dialogs had been addressed by the user.

However, I don’t know for certain this will work, and would be happy to be corrected.

Peter B.



with timeout of 100000 seconds
-- your script here
end timeout

The problem is not that a user doesnt respond. The System never attempts to shut down. There are no dialogs displaying “Save, Dont Save, Cancel”. In testing, Ive run the script manually and gotten the Save/ Dont Save prompt, and if I leave it indefinitely hanging at that point, it never goes away, and never brings on an applescript error. It just simply waits at that point indefinitely for someone to choose. Thats exactly what Im looking for.

This “AppleEvent timed out” error is not happening because of that, it must be happening at some earlier point since the “System Events” is never actually executing the command. Either something about the idle status is stopping the System Events from receiving the instruction in the first place or there is something else going on.

Maybe you should post the rest of your script… or at least the ‘on idle’ portion.

Peter B.


The script I posted is the complete script. Is it that I need an “on idle” part of the script in case the computer is idle? If so, could you explain how I’d modify my script to account for both the possibility that the computer might be idle and it might not?

Thank you for any help on this.

How are you triggering the script?

Peter B.


I designed an rsync script to back up selected folders and shut the computer down when it is done. The last, and only relevant part of that script is:

if [[ who | grep console = “” ]]
then
shutdown -h now
else
open -a /Library/Scripts/shutdown.app
fi

If the machine is not idle and I run the rsync script, the applescript triggers at the end as expected, and runs perfectly well, so I figured its not anything having to do with the shell script calling the applescript, but I defer to more knowledgeable minds when it comes to idle computers and calling applescripts saved as applications.

Once again, I’m in the position of having ‘bumped’ a post thread, and not having an adequate answer for the OP.

Using AppleScript only, I don’t know how to ‘define or delimit’ idle time… and my knowledge of shell scripting is minimal, so I can’t ‘read’ what you’re doing.

I hope someone else will step in at this point.

I think the added timeout bracket to your script might work… at least to prevent the AS timeout error.

It’s worth a try.

But again, I fully expect to be corrected by ‘more knowledgeable minds’.

Peter B.


all that shell script snippet says is if a user is not logged in then shutdown now, otherwise, open this application: /Library/Scripts/shutdown.app (which is my posted applescript)

For experiment sake, I will add the ‘with timeout’ bracket. But I have to admit Im skeptical. It seems as though that is just giving more time for whatever it is to respond. But nothing should be taking that long in the first place…so this doesnt address the underlying problem unless theres something Im not understanding. The fact that the script runs fine when triggered manually is the headscratcher. Its not like theres something in the script that is designed incorrectly. It seems like the idle status is affecting it.

Does anyone have any understanding of how idle status affects the calling and executing of applescript applications like mine?

For what little more it may be worth…

‘with timeout’ statements are often misunderstood as dictating that a script should require (or consume) x time to complete a given routine.

In fact, the said statement simply allows the script to stay ‘open’ (or active) for a time beyond the default time out.

Thus avoiding ‘Apple event timed out.’ errors.

Get it?

Peter B.


Yes, I understood it originally. My point remains that the script appears to be designed well enough to work under non-idle manually triggered circumstances. There is something affecting it when the machine is idle and the script is triggered on its own. I need to get at the root of that problem, rather then give it 100,000 seconds to complete when it shouldnt need more then whatever the default time is. If it cannot run or complete while the machine is idle, I will need to redesign it (possibly using ‘on idle’ which I dont really understand?) or I will need an entirely different solution, as giving it enough time for the idle status to be over when the user “wakes” the machine thus allowing the shutdown to complete doesnt make much sense. I am genuinely thankful for your attempt to help…it’s just that I’m skeptical this is the solution.

OK. So I tried the ‘with timeout’ block. I entered 100000 seconds as the amount of time before timing out and surrounded the entire script with the block. It did not work and still reported the “AppleEvent timed out” error. However, something noteworthy about what happened is that when I checked machines that had run the script only 15 minutes ago, the timeout error was on the screen. Perhaps thats a clue. If something within the script was timing out, the additional time shouldve stopped the reporting of an error for quite a long time. Is it possible that the “AppleEvent” that is timing out is the starting of the script itself? (due to machine being idle?) Here is the script with the added timeout block suggested by Peter.


with timeout of 100000 seconds
	set countdown to 300
	repeat countdown times
		tell me
			activate
			display dialog "YOUR COMPUTER WILL BE SHUTDOWN IN " & countdown & " SECONDS UNLESS YOU CLICK DO NOT SHUT DOWN!" giving up after 1 buttons {"DO NOT SHUT DOWN!", "OK, Please Shut Down now"}
			if button returned of result is "DO NOT SHUT DOWN!" then
				error number -128
			else if button returned of result is "OK, Please Shut Down now" then
				exit repeat
			end if
			set countdown to countdown - 1
		end tell
	end repeat
	
	tell application "System Events" to shut down
end timeout

Against my better judgement, I’ll take one last stab at this…

Here, on a 10.4.11 PPC machine, I can forestall the ‘Apple Event timed out.’ message using the basics of your script with the added timeout block.

But when I ran it (and ignored the repeated shutdown dialog) with an open document in TextEdit (and ignored the ‘Don’t Save/Cancel/Save’ dialog), yet another dialog popped up saying (something like) ‘Logout was interrupted by…’.

So it’s unclear to me what’s going on at your end… and I think I’ll retire from this particular fray with a couple of additional pointers.

From the AppleScript Language Guide (2008):

And from an authority on AppleScript whose post on a similar issue appears here…

http://bbs.macscripter.net/viewtopic.php?pid=81556#p81556

… though the whole thread is worth a read.

Sorry you’re still having trouble.

Peter B.


Hi Peter, thanks once again for attempting to help. Your last post led me to believe that maybe the problem is occurring within the “tell me” block since “with timeout” would not affect that portion of the script. So I redesigned it without a tell me block, having all commands inside a tell application “Finder” block. Finder can handle everything this script does. All commands now should be respecting the "with timeout of 100000 seconds…yet, I just ran a test and on the screen there was a “AppleEvent timed out” dialog. If I run the script manually, without the machine being idle, it completes just fine. Here is the reworked script:


with timeout of 100000 seconds
	tell application "Finder"
		activate
		set countdown to 300
		repeat countdown times
			display dialog "YOUR COMPUTER WILL BE SHUTDOWN IN " & countdown & " SECONDS UNLESS YOU CLICK DO NOT SHUT DOWN!" giving up after 1 buttons {"DO NOT SHUT DOWN!", "OK, Please Shut Down now"}
			if button returned of result is "DO NOT SHUT DOWN!" then
				error number -128
			else if button returned of result is "OK, Please Shut Down now" then
				exit repeat
			end if
			set countdown to countdown - 1
		end repeat
	end tell
	tell application "System Events" to shut down
end timeout

My newest idea: put each section of the script inside try statements with unique on error dialogs. This way I could see where the script is failing. That had no effect. I’m stumped…I still get the generic “AppleEvent timed out” message. Neither the “with timeout” or on error blocks seem to have any effect or ability to influence this problem.

Please help!

Here is the debug version of the script:


with timeout of 100000 seconds
	tell application "Finder"
		try
			activate
			set countdown to 300
		on error errText number errorNumber
			display dialog "activate, set countdown error: " & errText & errorNumber
		end try
		try
			repeat countdown times
				display dialog "YOUR COMPUTER WILL BE SHUTDOWN IN " & countdown & " SECONDS UNLESS YOU CLICK DO NOT SHUT DOWN!" giving up after 1 buttons {"DO NOT SHUT DOWN!", "OK, Please Shut Down now"}
				if button returned of result is "DO NOT SHUT DOWN!" then
					error number -128
				else if button returned of result is "OK, Please Shut Down now" then
					exit repeat
				end if
				set countdown to countdown - 1
			end repeat
		on error errText number errorNumber
			display dialog "repeat loop error: " & errText & errorNumber
		end try
	end tell
	
	tell application "System Events"
		try
			shut down
		on error errText number errorNumber
			display dialog "system event shut down error: " & errText & errorNumber
		end try
	end tell
end timeout

I’ve been following this thread with some interest, but haven’t been able to duplicate the problem. (I don’t know how to use rsync.) It’s not clear from the narrative that the AppleScript applet has actually been observed to run (in context) from the rsync script. If it’s not actually running, there’s obviously no immediate point in trying to fix it as the error’s occurring before then. :slight_smile: (This seems to be born out by your ‘try’ block attempts.) Is the applet visible in the dock when you see the error message? Is the message in a standard AppleScript error dialog?

I notice that the ‘open’ command in your rsync script contains a “-a” option with no corresponding “open with” parameter; but this doesn’t appear to make any difference, apart from producing a different error if the file doesn’t exist.