Re: 'with timeout' and scripting additions
Re: 'with timeout' and scripting additions
- Subject: Re: 'with timeout' and scripting additions
- From: kai <email@hidden>
- Date: Thu, 21 Apr 2005 16:04:28 +0100
On Thu, 21 Apr 2005 00:11:49 -0600, Donald Hall wrote:
According to Perry's Nutshell book, page 152, 'with timeout' should
work with a scripting addition command called inside a tell block that
targets another application.
For example:
[snip: script with dialog directly within Finder tell]
This works as expected if the dialog is left on the screen for more
than 5 seconds a timeout error is generated.
However, if I change the script to the following:
[snip: script with dialog within script object]
the timeout error does not occur, even though 'run script' is another
scripting addition.
I could have sworn this worked some years ago (OS 9 maybe). What
gives? What am I missing?
There may be a couple of issues involved here, Don.
As I understand it, a 'with timeout' statement applies to application
commands, as well as to scripting addition commands within tell
statements to application objects. However, such statements don't apply
to AppleScript commands or (significantly in this context) scripting
addition commands whose targets are not application objects.
In your first example, the 'display dialog' command is inside the
Finder tell block - as confirmed by the event log from this boiled-down
version of your script:
------------ Script:
with timeout of 5 seconds
tell application "Finder"
display dialog "this is a timeout test"
end tell
end timeout
------------ Event Log:
tell application "Finder"
display dialog "this is a timeout test"
current application
"Finder got an error: AppleEvent timed out."
------------
Sending the 'run script' command to a script object effectively places
the dialog call outside the tell block:
------------ Script:
script TimeoutTest
display dialog "this is a timeout test"
end script
with timeout of 5 seconds
tell application "Finder"
run script TimeoutTest
end tell
end timeout
------------ Event Log:
tell current application
run script «script TimeoutTest»
{button returned:"OK"}
end tell
------------
As Martin demonstrated, declaring the script object's parent property
as the Finder brings the application back into play. In fact, this
approach even renders the original tell block redundant.
------------ Script:
script TimeoutTest
property parent : application "Finder"
display dialog "this is a timeout test"
end script
with timeout of 5 seconds
run script TimeoutTest
end timeout
------------ Event Log:
tell application "Finder"
run script «script TimeoutTest»
current application
"Finder got an error: AppleEvent timed out."
------------
While one could imagine that simply using a Finder tell block within
the script object might do something similar, that's not the case here
(regardless of whether or not there's a tell block in the main script's
run handler):
------------ Script:
script TimeoutTest
tell application "Finder"
display dialog "this is a timeout test"
end tell
end script
with timeout of 5 seconds
run script TimeoutTest
end timeout
------------ Event Log:
tell current application
run script «script TimeoutTest»
{button returned:"OK"}
end tell
------------
For this to work, the 'with timeout' statement needs to be included in
the script object:
------------ Script:
script TimeoutTest
with timeout of 5 seconds
tell application "Finder"
display dialog "this is a timeout test"
end tell
end timeout
end script
------------ Event Log:
tell current application
run script «script TimeoutTest»
"Finder got an error: AppleEvent timed out."
------------
However, all this brings us to the second consideration to which I
referred earlier - in that a more appropriate command to send to a
script object might be the 'run' event, rather than 'run script' from
Standard Additions [1][2].
While this doesn't change the situation if the Finder tell block
remains in the calling handler, it does affect behaviour when it is
added to the script object:
------------ Script:
script TimeoutTest
tell application "Finder"
display dialog "this is a timeout test"
end tell
end script
with timeout of 5 seconds
run TimeoutTest -- or: tell TimeoutTest to run
end timeout
------------ Event Log:
tell application "Finder"
display dialog "this is a timeout test"
current application
"Finder got an error: AppleEvent timed out."
------------
Coming back briefly to the 'run script' command, the following script
doesn't timeout (obviously because 'TimeoutTest' doesn't include the
timeout statement):
------------ Script:
set TimeoutTest to "tell application \"Finder\"
display dialog \"this is a timeout test\"
end tell"
with timeout of 5 seconds
run script TimeoutTest
end timeout
------------ Event Log:
tell current application
run script "tell application \"Finder\"
display dialog \"this is a timeout test\"
end tell"
{button returned:"OK"}
end tell
------------
On the other hand, by including the 'with timeout' statement in the
script text, the following version will clearly timeout:
------------ Script:
set TimeoutTest to "with timeout of 5 seconds
tell application \"Finder\"
display dialog \"this is a timeout test\"
end tell
end timeout"
run script TimeoutTest
------------ Event Log:
tell current application
run script "with timeout of 5 seconds
tell application \"Finder\"
display dialog \"this is a timeout test\"
end tell
end timeout"
"Finder got an error: AppleEvent timed out."
------------
In fact, we've now come full circle - and the value of 'TimeoutTest' is
a replica of the very first script shown above.
Apologies for the plethora of examples.
---
kai
[1] As we've seen before ("persistence of script properties", March
2005),'run script' seems to run a separate instance of a script in
memory - which might help to explain why it behaves somewhat
differently to the 'run' command in this context.
[2] FWIW, I believe that in certain circumstances, a run script command
within a tell application "Finder" statement could even cause a crash
in previous versions of AppleScript. This was evidently fixed in
AppleScript 1.9.2 (Mac OS 10.3).
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden