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: "Donald S. Hall" <email@hidden>
- Date: Thu, 25 Oct 2001 23:50:13 -0600
>
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.
Thanks to everyone who took the time to reply. It appears there is no easy
way to tell if the script has returned a value that should be saved - even
if there is no return statement, the result will have something in it that
will be the "returned" value of the script. If the script ends with a return
statement without argument, the result will be undefined. Thus, I could have
scripts that DIDN'T want to save a returned value end in such a way and trap
for the error. However, in most cases the script will not want to save
anything, so shouldn't have to do something explicit to prevent this. It
would be better for the odd script that wants to save something to the file
to do something special. Since I want to save to my file as text only, I
could do the following:
set myVar to run script test
try
if myVar starts with ">" then
set valueToSave to (characters 2 thru -1 of myVar) as text
display dialog valueToSave -- for illustration only, write here
end if
end try
script test
set x to 1
if x = 2 then x -- this will leave 'result' undefined
--return ">" & x as text -- script that wants to save must use this form
end script
Note that the try block is needed in case the result should be undefined for
some reason after some script (like 'test' above) has run. Any script that
wants to save something should include a return statement like the commented
out one in the example. A bit contrived, but not too onerous for the script
writer!
Don
--
Donald S. Hall, Ph.D.
Apps & More Software Design, Inc.
http://www.theboss.net/appsmore
email@hidden