AppleEvent Timed Out

I’m trying to find out why I’m getting an “AppleEvent timed out” error from a script applet. The script is too long to post here, but here’s how it looks at the point where the error occurs:


	--write line0 in the log
display dialog "Choose one." buttons {"A", "B", "C"} default button 1 giving up after 60
if (button returned of result is "A") or (gave up of result is true) then
	--write line1 in the log
	--do something
	say "a"
else if button returned of result is "B" then
	--write line2 in the log
	--do something
	say "b"
else
	--write line3 in the log
	--do something
	say "c"
end if

The “AppleEvent timed out” error does not occur when I’m working on the Mac, even if I don’t interact with the script; it only occurs when the script runs unattended, but not always.

As it executes, the script writes to a log. When the time-out error occurs, the last line written in the log is line0, which indicates that the dialogue caused the error. But dialogues are not supposed to cause time-out errors; yet, even if they should, why cause a time-out, instead of gracefully giving up?

thanks, alex

If the display dialog is in fact the cause of the time out, you might be able to put it in a try loop so it won’t cause an error that will stop the script. It’s a difficult error as it seems to happen when you are not watching to see what’s going on.

try
display dialog "Choose one." buttons {"A", "B", "C"} default button 1 giving up after 60
end try

Why does your script ask for a user choice when it sounds like you intend for the script to run unattended? Perhaps there is a better way to code what you are trying to accomplish that would avoid the problem you are having.

Apple Events time out after 60 seconds unless you change the timeout. See “with timeout” for details:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.c9.html

Additional reading not required but recommended:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleEvents/index.html#//apple_ref/doc/uid/TP40001449

Thanks for your reply. I’m not sure that’s practical, but see below.

The idea is to give the user the option to cancel a lengthy procedure, if he’s around, while performing it automatically, if there’s no user input.

alex

Thanks for your reply. I don’t think that’s the real issue, but in one of the refs you provided (AppleEvents Programming Guide) I found a method of logging AppleEvents in Terminal, which helped me narrow down the problem. I’m not yet sure whether it’s an AppleScript bug, or a problem with my system.

The issue is reproducible. Save as an application the script I posted in my original post. If you launch it by double-clicking on it, it works as it should. If you launch it from the Scripts menu, or from the Terminal, and keep it frontmost, again, no problem. But, if you launch it from the Scripts menu, or from the Terminal, and immediately switch to another application, then the time-out error is eventually generated.

AppleEvent logging shows that the source is indeed the dialogue, which is an AppleEvent the applet sends to itself. The dialogue’s “giving up after” parameter should prevent the error, but in fact it makes no difference – in the circumstances I described, it is ignored.

alex

Update: It seems that inserting


tell application "System Events" to set frontmost of process "[applet name]" to true

before the dialogue forestalls the time-out error.

alex