Team LiB   Previous Section   Next Section
with timeout [of] {integer} second[s] end [timeout]

Syntax

With timeout of 15 seconds

End timeout

Description

The with timeout statement allows you to alter AppleScript's default 60-second time limit for the Apple events that are sent to applications. Normally, if an application fails to respond to an Apple event within 60 seconds, AppleScript raises an "Apple event timed out" error and stops running the script. You can make this time limit shorter, say 30 seconds, by using the syntax:

with timeout of 30 seconds...end timeout

You enclose the with timeout structure in a try block to trap and report any timeout errors (see "try"). with timeout only applies to the following types of commands. In other words, the with timeout limit is ignored unless the command is one of these types:

  • Commands sent to applications targeted in tell blocks

  • Scripting addition commands that have application objects as parameters (not too many osaxen have application objects as parameters)

  • Scripting addition commands that are called inside of tell statements that target other applications

Examples

The following example times out if you just let the display dialog dialog box sit there for over five seconds. This happens because the display dialog scripting addition is positioned inside the tell block targeting the Finder. Pull the scripting-addition command outside the tell block, and the script does not time out. Again, with timeout does not work with scripting-addition commands unless the command is part of a tell block targeting another application, or it takes an application object as a parameter:

try -- catch any timed out errors

   with timeout of 5 seconds

      tell application "Finder"

         get version

         display dialog "fast" (* let this sit for about 5 secs and raise 

         an error *)

      end tell

   end timeout

on error -- will be called if 'with timeout' block times out

   display dialog "Sorry, the operation timed out"

end try

If an AppleScript that sends an Apple event to another application times out, the Apple event itself is not cancelled (with or without a with timeout statement). So the script might have timed out, but the application could still eventually respond to the Apple event that the script sent to it.

    Team LiB   Previous Section   Next Section