• 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: variables vrs properties
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: variables vrs properties


  • Subject: Re: variables vrs properties
  • From: "Stockly, Ed" <email@hidden>
  • Date: Fri, 7 Jan 2011 12:56:20 -0600
  • Acceptlanguage: en-US
  • Thread-topic: variables vrs properties

Title: Re: variables vrs properties
Looking at this again, I realize that globals behave a little differently than I thought.  I believed that Jim’s example would fail, because the global is not declared inside the showDate handler, but it worked.

After a little experimentation I realized that when a global is declared at the top level of the script (either outside an explicit run handler, or in an implicit run handler) its value can be accessed inside other handlers without declaring it a global inside the handlers.  

Globals defined this way work just like properties.


However, globals defined within an explicit run handler (whether they are declared or not) must be declared inside a handler to access their value.

I did not realize there was any difference between declaring globals in explicit or implicit run handlers, but there is.

ES

-----------------
--global not declared in run handle, not declared in handlers
--Fails
on run
   set dd_mmm_yyyy to "Global display" as text
   my showDate()
end
run
on showDate()
    
tell
application "Finder" to display dialog dd_mmm_yyyy
end showDate
-----------------

-----------------
--global not declared in run handle, declared in handlers
--works
on run
   set dd_mmm_yyyy to "Global display"
    
my
showDate()
end
run
on showDate()
    
global
dd_mmm_yyyy
   display dialog dd_mmm_yyyy
end showDate
-----------------
--global declared at top level of script with explicit run handler, not declared in handlers
--
works!!
global dd_mmm_yyyy
on run
   set dd_mmm_yyyy to "Global display"
    
my
showDate()
end
run
on showDate()
    
display dialog dd_mmm_yyyy
end showDate

-----------------
--global declared at top level of script, not declared in handlers
--works!!
global dd_mmm_yyyy
set dd_mmm_yyyy to "Global display"
my
showDate()
on
showDate()
    
display dialog dd_mmm_yyyy
end showDate

--
---------------

-----------------
--global declared somewhere in implicit run handler, not declared in handlers
--works!!
set dd_mmm_yyyy to "Global display"
global
dd_mmm_yyyy
my showDate()
on
showDate()
    
display dialog dd_mmm_yyyy
end showDate

--
---------------

On 1/6/11 6:27 AM, "Jim Brandt" <email@hidden> wrote:

When I've used globals, I've always defined them at the top of the script.

I can then use them in a handler later.

For instance:

global dd_mmm_yyyy
set dd_mmm_yyyy to (do shell script ("date \"+%e-%b-%Y\"") as text)

my showDate()

on showDate()
tell application "Finder" to display dialog dd_mmm_yyyy
end showDate

So unless showDate was in a library, the global doesn't need to be defined within the handler.
 _______________________________________________
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

References: 
 >Re: variables vrs properties (From: Jim Brandt <email@hidden>)

  • Prev by Date: Re: Increasing numbers
  • Next by Date: Re: Slow vs faster
  • Previous by thread: Re: variables vrs properties
  • Next by thread: slow shell
  • Index(es):
    • Date
    • Thread