Re: Global variables
Re: Global variables
- Subject: Re: Global variables
- From: deivy petrescu <email@hidden>
- Date: Thu, 8 Mar 2007 13:28:24 -0500
On Mar 8, 2007, at 11:02, Larry the O wrote:
OK, I must be misunderstanding something really basic here.
All I'm trying to do is create a droplet that starts out by
declaring and initializing a bunch of global variables, then runs a
series of subroutine calls to do the work. The idea of using the
global variables is so that the subroutines can read and write the
global variables without them having to be explicitly passed in the
subroutine call, right?
What I believe you are misunderstanding is, you are assuming that
declaring the variables in the first handler makes the variables
properties.
First, to make the variable global declare it in the script main
body. No need to declare it as global, it is global by default. You
can put your variable anywhere on the script, but before the calls to
the handlers (this should be kinda logic).
Then, declare the variables you are going to use in the handler, in
the handler!
That is it.
Not working. It worked when I was writing the code and running it
directly from the script window, but once I added the droplet front
end (which was independently tested and seems to work fine), the
global variables are no longer working.
I am hard pressed to believe that the script below worked. I have not
tried with an open handler, I'll do it following this message, but I
see no way your other handlers would see the variables from the
first, if they are not declared as global.
As far as I can check, by default a variable declared on the top
level script is global by default (except loop variables) and a
variable declared at a handler level is local by default.
The actual code is kind of messy right now, so, for the moment,
makePretend code:
This works as (you) expected:
<script>
set littleMess to {"one"}
set bigMess to "bigMess"
set ohMiGod to "toilet paper"
dosome()
on dosome()
bumpAndGrind()
twistAndShout()
shakeAndBake()
end dosome
--
on bumpAndGrind()
global littleMess
display dialog item 1 of littleMess as string
set littleMess to {"incontinent dog", "naughty cat"}
display dialog item 1 of littleMess as string
end bumpAndGrind
--
on twistAndShout()
global bigMess
display dialog bigMess
if bigMess is "bigMess" then
set bigMess to "Uncle Ben's Rice"
end if
end twistAndShout
--
on shakeAndBake()
global littleMess
global bigMess
global ohMiGod
display dialog ohMiGod
display dialog bigMess
if ohMiGod is "toilet paper" then
set bigMess to "You don't want to know."
else
set littleMess to {"cleanup effort", "disaster recovery"}
end if
display dialog bigMess
end shakeAndBake
</script>
Deivy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden