• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Scope of globals, parents and children, "load script" and all that jazz
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scope of globals, parents and children, "load script" and all that jazz


  • Subject: Re: Scope of globals, parents and children, "load script" and all that jazz
  • From: Arthur J Knapp <email@hidden>
  • Date: Thu, 17 May 2001 11:00:08 -0400

> Date: Thu, 17 May 2001 05:21:06 +0200
> From: Sander Tekelenburg <email@hidden>
> Subject: Scope of globals, parents and children, "load script" and all
> that jazz

> I'm trying to reorganize ... by separating it into a main script (parent)
> and ... into a separate script library (child) that is called by the main
> script with the "load script" command.

> However, I ran into something that tells me I seem to have problems
> understanding the scope/meaning of globals.

You and me and every AppleScripter that has ever lived... ;-)


> ... The original ('all-in-one') script uses a lot of globals (17).
> ... in order to get everything to work, I needed to do 2 things

> 1) declare those same globals again, at the top of the script
> 2) declare them yet again within each handler! (well, only those globals that
> apply to that particluar handler)

I ran some experments, (Length warning):


-- file "ChildScript"

on UseIt()
return gVariable
end UseIt

on UseMy()
return (my gVariable)
end UseMy

on UseGlobal()
global gVariable
return gVariable
end UseGlobal

on run
set {byItself, asMy, asGlobal} to {0, 0, 0}

try
set byItself to gVariable
end try

try
set asMy to my gVariable
end try

try
global gVariable
set asGlobal to gVariable
end try

return {byItself, asMy, asGlobal}
end run




-- file "MainScript"

global gVariable

set gVariable to "Hello World"

property LoadedChild : load script ,
alias "Commander:Desktop Folder:temptemp:ChildScript"

script LocalChild

on UseIt()
return gVariable
end UseIt

on UseMy()
return (my gVariable)
end UseMy

on UseGlobal()
global gVariable
return gVariable
end UseGlobal

on run
set {byItself, asMy, asGlobal} to {0, 0, 0}

try
set byItself to gVariable
end try

try
set asMy to my gVariable
end try

try
global gVariable
set asGlobal to gVariable
end try

return {byItself, asMy, asGlobal}
end run

end script

run of LoadedChild
-- > { "Hello World" , 0 , "Hello World"}
-- byItself , asMy , asGlobal
(*
* This tells us that the loaded script was able to
* refer to gVariable with no problem as a normal
* variable within it's run handler, as well as to
* explicitly refer to it as a global.
*)

run of LocalChild
-- > {"Hello World", "Hello World", "Hello World"}
-- byItself , asMy , asGlobal
(*
* The local script object can refer to the global
* at it's top-level in any way that it wants. The
* only difference between the top-level of a local
* script object and a loaded script object is with
* regards to the "my" or "of me" syntax. I assume
* this has something to do with the fact that the
* loaded script object is "initialized" with it's own
* "context", where as a local script object is
* initialized with access to the main script, and
* therefore it can "inherit" globals of the main
* script.
*)

(*
UseIt() of LoadedChild
*)
-- > error "The variable gVariable is not defined."
UseIt() of LocalChild
-- > "Hello World"
(*
* Within a handler, the loaded script object cannot
* simply refer to a global of the main script, while
* the local script object can.
*)

(*
UseMy() of LoadedChild
*)
-- > error "Can't make some data into the expected type."
UseMy() of LocalChild
-- > "Hello World"
(*
* When saying "my gVariable", or "gVariable of me", the
* loaded script object doesn't seem to consider the main
* script to which it now belongs to be a part of it's
* "inheritence" chain, where as the local child does seem
* to think of the main script as it's implicit parent, and
* therefore it "inherits" it's globals.
*)

UseGlobal() of LoadedChild
UseGlobal() of LocalChild
-- > "Hello World"
(*
* Both have no difficulties in using a global within
* a handler, once the variable has been explicitly
* declared as global.
*)



> (In the long run I will probably split the library up into several libraries.
> And I would like to get rid of as many globals as I can. But for now I was
> just trying if I could easily switch to this construction without having to
> get rid of all globals just yet.)

I have recently been doing some work with script objects and their
advanced features, but I have mostly confined myself to working within
one script file. Ultimately, I would want to separate the various objects
into their own script files to make them more manageable. I wish that
the whole "scope" issue of AppleScript wasn't quite so complicated...



Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden

Hey, check out:
http://www.vivaladata.com


  • Follow-Ups:
    • Re: Scope of globals, parents and children, "load script" and all that jazz
      • From: Sander Tekelenburg <email@hidden>
  • Prev by Date: Re: Time Delay with AppleScript
  • Next by Date: Re: Time Delay with AppleScript
  • Previous by thread: Re: Time Delay with AppleScript
  • Next by thread: Re: Scope of globals, parents and children, "load script" and all that jazz
  • Index(es):
    • Date
    • Thread