• 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: *Very* strange script / global variable behavior
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: *Very* strange script / global variable behavior


  • Subject: Re: *Very* strange script / global variable behavior
  • From: Paul Berkowitz <email@hidden>
  • Date: Sun, 13 Mar 2005 14:08:41 -0800

Title: Re: *Very* strange script / global variable behavior
On 3/13/05 11:48 AM, "Axel Luttgens" <email@hidden> wrote:


By contrast, should someone have asked why following code errors,
everybody would have said "hey! that's normal! you wouldn't want to be
able to clash local variables that way, would you?":

    -- Code4
    set SomeProp to "xyz"

    script LogIt
        log SomeProp
    end script

    tell LogIt to run
    --> Error: variable SomeProp is undefined

So, everyone would understand the error produced by Code4 because of
basic and almost universal programming rules.




Hmmm? SomeProp is not a local variable.
I disagree ;-)
Within Code4, SomeProp designates a entity local to LogIt's implicit run handler.
As well as a global variable implicitely declared at the script's top-level.
And those two beasts, the local one and the global one, really are distinct.

It is only when other scoping hints are provided, such as in your Code4A or in my Code1bis, that identifier SomeProp may be brought to designate the same global entity.

But in order for Code4A's log command to be logging (*xyz*), it means that the global SomeProp just declared in LogIt script object has gone out to the top level and "found" an existing SomeProp value. It couldn't do that if SomeProp did not have a quasi-global namespace already - in other languages you'd get a "undefined" error. Try this:

---Code4B

script DefineIt
   set SomeProp to "xyz"
end script

script LogIt
   global SomeProp
   log SomeProp
end script

tell DefineIt to run
tell
LogIt to run

---


Here the log command errors "
The variable SomeProp is not defined" since the global declaration in LogIt could not find any existing SomeProp variable anywhere, even though DefineIt had already run. That's because the line setting SomeProp has truly local scope only when defined. When it's defined at the top level, however, as in Code4A, the namespace is exposed to all inner script objects and handlers, even if they "ignore" it if it's not declared global somewhere available (in the particular script object in question, or again at the top level). There's a strange semi-global sate that makes the namespace global but not the value. Another AppleScript-only oddity.

--
Paul Berkowitz
 _______________________________________________
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

  • Follow-Ups:
    • Re: *Very* strange script / global variable behavior
      • From: Axel Luttgens <email@hidden>
    • Re: *Very* strange script / global variable behavior
      • From: Neil Faiman <email@hidden>
References: 
 >Re: *Very* strange script / global variable behavior (From: Axel Luttgens <email@hidden>)

  • Prev by Date: Re: Scripting Energy Saver to Sleep Display Now?
  • Next by Date: Re: *Very* strange script / global variable behavior
  • Previous by thread: Re: *Very* strange script / global variable behavior
  • Next by thread: Re: *Very* strange script / global variable behavior
  • Index(es):
    • Date
    • Thread