• 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: Handler query - exchanging values
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Handler query - exchanging values


  • Subject: RE: Handler query - exchanging values
  • From: "Stockly, Ed" <email@hidden>
  • Date: Fri, 30 Jun 2006 12:31:25 -0700

>>Axel Luttgens>>
While not that drastic (well written scripts would not have been
affected), it would have been a (very, very) slight move towards the right direction as far as AppleScript's scoping rules are concerned.A slight one, as lots of other questionable behaviors would have  persisted anyway.




Of the scoping behavior Axel spelled out below, I would argue there is only one example that I would consider "questionable".  All the others are useful, logical and once they've been mastered, fairly straightforward.


All variables in the top level of a script are global in scope to all handlers withing the script, even if not declared as globals in the top level.

A handler can access a global variables if the global is declared within the handler.

Properties must be delcared in the top level of a script and are global in scope.

A handler may access a property.


>>>Above examples might look rather surprising to people not familiar with AppleScript, wouldn't they?

Yes. Here's the thing. AppleScript is fairly intuitive, but not totally intuitive. It does not require as much study as C programming or shell scripting, but (as with any programming and scripting language) it does have behaviors that may not seem consistent or may seem arbitrary. However, once you've learned how the scopes of variables are applied, they're fairly easy to master.


That said, the examples provided that are inconsistent and illogical are these two:

   -- Again, apples is local to boo().
   on boo()
       set apples to "set in boo()"
   end boo
   global apples
   set apples to "set in implicit run handler"
   boo()
   apples
      --> "set in implicit run handler"

   -- And here, apples is non local because of lexical scoping.
   global apples
   on boo()
       set apples to "set in boo()"
   end boo
   set apples to "set in implicit run handler"
   boo()
   apples
      --> "set in boo()"

IMHO, it shouldn't matter if a global declaration (or setting a variable's value) in the top-level of a script comes before or after the appearance of the handler the behavior should be the same.

ES
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Summary: Deselecting the selection in the finder window
  • Next by Date: Re: Photoshop Help
  • Previous by thread: Re: Handler query - exchanging values
  • Next by thread: Create an email with an attachment
  • Index(es):
    • Date
    • Thread