Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 01-23-2011, 05:09 AM   #1
david-bo
Triple-A Player
 
Join Date: May 2003
Posts: 210
Initial value for global variables

Is this really necessary?

Code:
global chars
set chars to {"a", "b", "c"}
I would like to do it in one line, something like

Code:
global chars {"a", "b", "c"}
Properties can be declared in a similar manner

Code:
property chars : {"a", "b", "c"}
I am trying to achieve a constant global list. What is the best way to do this?
__________________
Koala Surf Camp
david-bo is offline   Reply With Quote
Old 01-23-2011, 07:07 AM   #2
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
Are properties not global?
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 01-23-2011, 07:15 AM   #3
david-bo
Triple-A Player
 
Join Date: May 2003
Posts: 210
Yes, but as I understand it - if you change a property first time you run a script, this changed value will apply next time the script is run. In other words, it won't be reset.
__________________
Koala Surf Camp
david-bo is offline   Reply With Quote
Old 01-23-2011, 11:51 AM   #4
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
Will the value be changed in the script?
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 01-23-2011, 03:15 PM   #5
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Quote:
Originally Posted by david-bo
Is this really necessary?

Code:
global chars
set chars to {"a", "b", "c"}

Apparently it is. But the only time it would matter (i think) is when the script has both a run handler and an open handler... and the run calls the open.

Quote:
Originally Posted by david-bo
I am trying to achieve a constant global list. What is the best way to do this?

The way i do it (for the case i just mentioned) is to create a handler called INIT() and have both handlers call it right off the bat. Yes... there is a degree of redundancy involved, and maybe an if/then will be needed to avoid having the open handler trample on some values already set by the run handler.

It all depends on what exactly the script does i s'pose.
__________________

chown -R us /your\ base/

-HI-

.
Hal Itosis is offline   Reply With Quote
Old 01-23-2011, 03:26 PM   #6
david-bo
Triple-A Player
 
Join Date: May 2003
Posts: 210
Quote:
Originally Posted by renaultssoftware
Will the value be changed in the script?

Not in the current version. However, I have learned the hard way that constants should be constant. Sometime in the future one modifies the code and it breaks in an unexplainable manner.
__________________
Koala Surf Camp
david-bo is offline   Reply With Quote
Old 01-23-2011, 03:36 PM   #7
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 506
The global and local keywords describe the scope, but they don't initialize the variable. A property is a little bit of a different beast, more of an object characteristic (you can access a property from another script, for example), so it is initialized with a class and a value.
__________________
MacBook Pro / OS X Mountain Lion (10.8.3) / Xcode 4.6 / [various (much) older stuff keeping dust off the shelves]
Red_Menace is offline   Reply With Quote
Reply

Tags
applescript, declaring variable, global variable, list, property

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:05 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.