Re: global variable not being recognized by script object subroutine
Re: global variable not being recognized by script object subroutine
- Subject: Re: global variable not being recognized by script object subroutine
- From: JollyRoger <email@hidden>
- Date: Tue, 27 Feb 2001 21:01:53 -0600
Welcome. Glad to have helped. =)
JR
on 2/27/2001 7:56 PM, Bill Planey at email@hidden wrote:
>
This is EXACTLY what I needed!!!!
>
>
I don't know if I missed it in the 4 or 5 AppleScript books, but
>
nowhere was it explained that it is not enough to SET a variable
>
in the origin code, but you must also MARK the invokation of it
>
(by simply putting "global VariableName" on a line above it...)
>
in any external handlers/script objects (not merely reference the
>
variable by its name...
>
>
THANKS!!!!!!!!!!!!!!
>
>
Bill Planey
>
>
> on 2/27/2001 2:10 PM, Bill Planey at email@hidden wrote:
>
>
>
>> I did another test, and moved the handler from the script object to a
>
> physical
>
>> location within the master script (outside the on run/end run statements).
>
It
>
>> still didn't work when called.
>
>>
>
>> I fear I am doing something wrong that betrays my relative beginner status
>
>> with AppleScript.
>
>
>
> Hi Bill,
>
>
>
> The current variable scope changes depending on where you use the variable.
>
> Handlers have their own scope which is different than the global scope. By
>
> default, handlers only know about variables in their local scope. Unless
>
> you specify that a variable is in the global scope, the handler will assume
>
> that the variable is in the handler's local scope. Your handler is trying
>
> to use a variable named AnyDesktop in the handler's local scope, and is
>
> properly failing.
>
>
>
> In the following script, SecondHandler() tells AppleScript that the variable
>
> AnyDesktop is in the global scope, so that the handler will access the
>
> variable globally.
>
>
>
> my Firsthandler()
>
> my SecondHandler()
>
>
>
> on Firsthandler()
>
> global AnyDesktop
>
> set AnyDesktop to path to desktop folder as string
>
> end Firsthandler
>
>
>
> on SecondHandler()
>
> -- if you uncomment the following line, SecondHandler will access the
>
> AnyDesktop variable in the global scope
>
> -- global AnyDesktop
>
> display dialog AnyDesktop
>
> end SecondHandler
>
>
>
> HTH
>
>
>
> JR