Re: any way to tell if a script has a 'return' statement?
Re: any way to tell if a script has a 'return' statement?
- Subject: Re: any way to tell if a script has a 'return' statement?
- From: has <email@hidden>
- Date: Thu, 25 Oct 2001 20:32:34 +0100
Donald S. Hall wrote:
>
If you run a script with 'run script', the script can optionally return a
>
value in the result variable with a return statement.
>
>
Is there any way to determine if a script in fact has a return statement?
>
>
I want to be able to save the result returned by a script to a file, but
>
since the result will always have something in it (something I don't want to
>
save if the script doesn't specifically return a value), how do I tell
>
whether or not I should be saving the result variable? I won't know a priori
>
if the script I am running has a return statement.
I'm wondering if you're a little confused about how and when values
get/don't get returned...
If a script returns a value at its end, then running it via "do script"
will likewise return a value. If no value is returned by a script, running
it via "do script" won't return a value either. For example:
set returnedValue to do script "set x to 1
if x = 1 then x"
-->1
set returnedValue to do script "set x to 2
if x = 1 then x"
-->Error message: No result was returned from some part of this
expression.
Course, if you're expecting a value to be returned and one isn't, you'll
get an error - which you can trap if necessary.
Now, where I think some confusion might exist...
You'll also note that I didn't use "return x" in the above - although
"return" is useful when you want to return a value early, a value may still
be returned when AS reaches the end of the script - depending on whether or
not there is a variable kicking around there, left over from the last
command. For example:
set returnedValue to do script "set x to 2
if x = 1 then return x
display dialog \"Value x wasn't returned.\""
-->{button returned:"OK"}
Which you don't want. (Bad script. Bad script.) If you're concerned about
what gets returned, you'll need to be more careful about what appears at
the end of a script. The solution's simple, simply add "return" to the end
to make sure no value is returned:
set returnedValue to do script "set x to 2
if x = 1 then return x
display dialog \"Value x wasn't returned.\"
return"
-->Error message: No result was returned from some part of this
expression.
Again, remember to trap this error by wrapping the whole "set
returnedValue..." line in a try statement.
has