Author Topic: Lesson #2.3 : Variables and Properties  (Read 1737 times)

0 Members and 1 Guest are viewing this topic.

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Lesson #2.3 : Variables and Properties
« on: June 11, 2007, 09:09:52 AM »


2.3 Variables and Properties

Variables:
We all use variables in our day-to-day life. Keeping with our “sentence” analogy, variables would be equivalent to pronouns. Take the following sentence for example:

My wife and I bought a new house in the country; we got a great deal on it and I am sure the kids will like living there.

Without much thinking on your part, you will automatically associate:
We to My wife and I
It to house
There to in the country

There is an obvious reason why we talk  (and write) this way: to not have to re-define each part of our sentence every time we need to use it. In a programming language, variables act in the same manner and can be viewed as special “drawers” where you keep distinctive pieces of data.

With most advanced programming languages, you have to determine the size and types of a variable before you can use it. That is not the case here. In AppleScript, you define a variable simply by giving it a value. Doing this can be done using either the “set” or the “copy” commands (those commands are pretty much equivalent aside from a small distinction that we will see in another lesson):

Quote
001     set myVar to "Hello"
002     copy (3 * 6) to myOtherVar

There are a few rules when you think about naming your variables:
  • Cannot start with a number
  • Can only contain the following characters: letters of the alphabet (both uppercase and lowercase), the numerals 0 through 9, and the underscore character “_”
  • Cannot contain spaces
  • Cannot be the same as a reserved word
Note: You can “bypass” the last 2 rules if you enclose your variable name between vertical bars  (“|”)

Quote
001     set OneNumber to 10
002     set One_Number to 10
003     set |1number| to 10
004     set |One Number| to 10
005     set |number| to 10

A quick note on naming convention. It is up to you as the developer to name your own variable, however, I would suggest you choose meaningful names for them. This will save hours upon hours of debugging and updating time. Consider the following lines:

Quote
001     --Vague
002     set X to 10
003     set Y to 4
004     set Z to (X * Y)

005     set a to date "Friday, June 8, 2007 1:00:00 PM"
006     set b to round (((current date) - a) / days)

007     --Meaningful
008     set Price_of_Article to 10
009     set BoughtQty to 4
010     set TotalAmountOfTheTransaction to (Price_of_Article * BoughtQty)

011     set date_Lastmeeting to date "Friday, June 8, 2007 1:00:00 PM"
012     set int_DaysSinceLastMeeting to round (((current date) - date_Lastmeeting) / days)

While they both do the same thing, it is much easier to determine what the second group will do. Think about a few years from now when you will need to revise this script. If you chose the latter example, you will find it much easier to understand what the code is actually doing (that is not taking into account the fact that your scripting skills will improve over time which will leave you thinking: what the heck was I thinking about?).


Scope:

Simply put, the scope of a variable determines where it can be used or where it will be known to exist.
You might be a figure of authority in your home but have very little, or no authority, in someone else’s, in that case, your scope is limited to your home.

For variables, the scope is mainly function of where they are declared. To get a better understanding of this, you have to know about the different “parts” of any given script, which basically can be separated in three groups.

Quote
001     --Main Area
002     property pty_Value : 2
003     global TestValue

004     set TestValue to pty_Value
005     my Handler_Sub1()
006     run Child_Script
007     my Handler_Sub2()

008     display dialog "MainScript " & TestValue


009     -- Handlers Area
010     on Handler_Sub1()
011          local TestValue
012          set TestValue to 3 * pty_Value
013          display dialog "MainSub1 " & TestValue
014     end Handler_Sub1

015     on Handler_Sub2()
016          display dialog "MainSub2 " & TestValue
017     end Handler_Sub2

018     -- Script Object Area
019     script Child_Script
020          --SO Main Area
021          property pty_Value : 3
022          local TestValue
     
023          set TestValue to pty_Value
024          my Handler_Sub()
     
025          display dialog "ChildScript " & TestValue
     
026          -- SO Handlers Area
027          on Handler_Sub()
028               local TestValue
029               set TestValue to 4 * pty_Value
030               display dialog "ChildSub " & TestValue
031          end Handler_Sub
032     end script

  • First you have what I call the Main Area. This is the default area you get when you open a new script window. When run, that is where the Script Editor will look for lines of codes to be interpreted.
  • Next you have the Handlers Area(s). These are the line used for declaring a handler or sub-routine. Your Script Editor will interpret those only if a call is made to them from elsewhere in you script.
  • Finally you have the Script Object Area(s). You will learn in a later course that your script can have their own scripts (complete with their own variables and properties, and, their own areas as well.)
As you can see from the example you can declare variables to have a global or local scope. If not specified, your variables will be declared locally. Declaring a "global" variable from a Script Object of a Handler will poll the "parent" for the value of that variable if it exists.

Note here that there is a Hierarchy involved. The Main Area (the script itself) have control over the Handlers as well as other Scripts Object. A Script Object has only controls over its own Handlers and Script Objects. And finally, a Handler only has control over itself. So, declaring a global variable inside the Main Area will “publish” that variable to its enclosed Handlers and Script Objects.

You can override the scope of a variable but this ONLY affect the “parts” controlled by the area where this is done.


Properties:

Properties act and feel the same as any variable do; they can be change in value of type at any time during the processing of your code. There are however three big differences with them:
  • Properties must be declared and given a default value. This can only be done at the beginning of a script.
  • Properties are all global by definition.
  • Properties are saved with the script. This means that if your code changes the value of a property during its process, the next time that script will be run that changed value will remain in effect.
    Note: Every time you will recompile your code, the properties will revert to the default values you have given them.

to declare a property you must use the command word property or prop followed by the name you wish to give it in turn followed by a colon and the default value for that property:

Quote
001     property pty_RunCount : 0
002     property pty_LastRun : current date



« Last Edit: June 11, 2007, 04:17:41 PM by Publi-Script »
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5