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

Re: Global variables


  • Subject: Re: Global variables
  • From: deivy petrescu <email@hidden>
  • Date: Fri, 9 Mar 2007 10:56:12 -0500


On Mar 8, 2007, at 16:13, Stockly, Ed wrote:


deivy >>> 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.

That's not exactly correct. The script's main body is not executed if the script is used as a droplet, so undeclared globals are not globals when run as a droplet. But globals declared in the top level are indeed globals, so you must declare globals in the top level of your script if you're going to use it as a droplet.


That's one reason why I think properties are easier to use than globals. They work one way no matter the context.

HTH,


ES

Ed,
what I wrote is correct.
A variable declared at the top level is global and has its value retained across runs.
As I pointed out, if the declaration comes after the call, one gets an error. This is what happens in a droplet.
I run some tests with droplets here and declaring global or not, makes no difference, the variable will not be defined if it is set in the top level script.
Property, however, is set at compilation time and will pass the value to the handler wether or not the top level script is run.
I agree with you that it is better to declare a property than a global variable, if I am to declare it at the top level. However, the fact that a declared variable is global, has some utility. If you need it in only one handler, declaring it a global in that handler allows it to be used without being passed.
Interestingly enough, this might also cause some problems. As pointed out below:


<script>
set j to 1

repeat with j from 1 to 10
	set soma to j
end repeat

log j -->j=10
if soma < 11 then get NeedMoreLoops(soma)


on NeedMoreLoops(soma) global j log j -->j=1 repeat with j from j to j+1 set soma to soma + j end repeat end NeedMoreLoops

--> 13 (not 31)
</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
References: 
 >RE: Global variables (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: how to do a proper alphabetical sort?
  • Next by Date: Scripting TextEdit and saving a file without user intervention
  • Previous by thread: RE: Global variables
  • Next by thread: RE: Global variables
  • Index(es):
    • Date
    • Thread