RE: Global variables
RE: Global variables
- Subject: RE: Global variables
- From: "Stockly, Ed" <email@hidden>
- Date: Thu, 8 Mar 2007 13:13:58 -0800
- Thread-topic: AppleScript-Users Digest, Vol 4, Issue 107
>>>I must be misunderstanding something really basic here.
I wouldn't call this basic, it's probably more complex than it should be and not everything works the way it's supposed to or the way it worked before...
>>>All I'm trying to do is create a droplet that starts out by declaring and initializing a bunch of global variables, then runs a series of subroutine calls to do the work.
Very good, sensible approach. (although some "old school" programmers will argue that it's more efficient to use local variables passed to handlers rather than globals, but feel free to ignore them)
I agree with the others who responded about Globals vs. Properties. It has been my experience that properties work better, are easier to grok and are more reliable than globals, particularly in droplets.
Rather than:
global littleMess
global bigMess
global ohMiGod
<snip>
use
property littleMess: {}
property bigMess: ""
property ohMiGod "toilet paper"
on open droppedFiles
<yadda yadda yadda>
You never have to declare the property inside any handlers to get its value and you can set its value when it's declared. Keep in mind that your droplet will remember the values of both properties and globals between runs, but not local variables.
>>Yvan>> From my point of view, your code met two problems.
>>>- 1 - It is written as a droplet...
That's not a problem if you intend to use it as a droplet. However, if you use an open handler in your script you need to include a variable that will be set to the aliases of the items dropped on the icon. (I'm guessing that you didn't include that in your example but did in your actual script)
on open docList --works
on open --- doesn't work
>>>>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
_______________________________________________
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