Author Topic: Lesson #2.4 : Data Types  (Read 3621 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.4 : Data Types
« on: June 11, 2007, 04:16:12 PM »


2.4 Data Types

I mentioned that with AppleScript you do not have to specify the data type. This does not change the fact that all of your variables WILL be of a certain data type. Let’s go through them shall we (well the most common ones in any case)
  • Boolean: This type can only hold one of two pieces of information stored as a single bit of data (0 or 1). With AppleScript you will see them as holding a true or false value.

    Quote
    set x to 3 > 4
    -->     false

    AppleScript will evaluate the expression “3 is greater than 4” and return a value of false. So in that example, x is false. You will quickly learn the value of Boolean and find it as hard to write a script without it as it to write a sentence with no “e”.

  • Date: Unlike what its name would suggest the date type is really speaking of a specific TIME and DATE.

    Quote
    set aDate to current date
    -->     date "Monday, June 11, 2007 4:44:07 PM"

    The value of current date is a date identifiable by having the word “date” followed by a string describing the date in question. You can also “invent” dates and assign them to variable in the following manner:

    Quote
    set DatePart to "02/05/07"
    set aDate to date DatePart
    -->     date "Monday, February 5, 2007 12:00:00 AM"

    As you can see, you do not have to supply every “part” of a date, AppleScript will infer the rest from the current date values. This is nice since it would be quite cumbersome to have to constantly verify on which day of the week any given date falls. As you can see, AppleScript can resolves your attempts at creating dates that cannot possibly exist (within limits of course)
    Note: Assigning year values only using 2 digits. The behaviour will differ depending on when you write your script.


    A) If you write your script at the end of a century (xx91-xx99), AppleScript will deem years 11 to 99 to be in the current century, years 00 to 10 will be deem to be in the next century.
    B) If you write your script in the middle of a century (xx11-xx90), AppleScript will deem years 00 to 99 to be in the current century.
    C) If you write your script at the beginning of a century (xx00-xx10), AppleScript will deem years 00 to 90 to be in the current century, years 91 to 99 will be deem to be in the last century.

    You will find out later that you can have AppleScript compile script at runtime; this can complicate things quite a bit when using dates. Better be safe than sorry and always use 4 digits.

    Date properties:
    Should we have only that date format to contend with, we wouldn’t be using it all that much, so let’s see what we can do with it. Here are the properties associated to a date object:
    • day: The integer (1-31) specifying the day of the month.
    • weekday: One of the following 7 constants Monday or Mon, Tuesday or Tue, Wednesday or Wed, Thursday or Thu, Friday or Fri, Saturday or Sat, Sunday or Sun.
    • month: One of the following 12 constants January or Jan, February or Feb, March or Mar, April or Apr, May, June or Jun, July or Jul, August or Aug, September or Sep, October or Oct, November or Nov, December or Dec.
    • year: The integral value of the year. Ex.: 2007.
    • time: The integral value of the amount of seconds that elapsed since 12 AM (Midnight) of the evaluated date object.
    • date string: The string defining the “date” portion of the evaluated date object; for example, "Saturday, June 09, 2007".
    • time string: The string defining the “time” portion of the evaluated date object; for example, "4:37:21 PM".

    Date constants:
    When working with date objects, you will quickly find yourself wanting to calculate a new date based on another one. All arithmetic done on date are using the second as the universal unit for dates:

    Quote
    set aDate to date "Monday, June 11, 2007 12:00:00 AM"
    set bDate to date "Thursday, June 11, 1992 12:00:00 AM"
    set Date_Difference to aDate - bDate
    -->     473299200

    But this does not look very usefull or handy. To help us in our task, AppleScript has introduced a few constants that greatly simplify things:
    • seconds: Integer, evaluates to 1
    • minutes: Integer, evaluates to 60 (or 60 * seconds)
    • hours: Integer, evaluates to 3600 (or 60 * minutes)
    • days: Integer, evaluates to 86400 (or 24 * hours)
    • weeks: Integer, evaluates to 604800 (7 * days)
    note: There are no constant for either Months or Years as their value (in seconds) vary from one to the next.

    Quote
    set aDate to date "Monday, June 11, 2007 12:00:00 AM"
    set bDate to date "Thursday, June 11, 1992 12:00:00 AM"
    set Date_Difference to (aDate - bDate) / (365 * days)
    -->     15.008219178082

    As you can see, a little more of 15 years span between these two dates. this is du to the fact that (365*days) does not take leap years into consideration.

  • List: A List is an ordered series of values of any type bound together by accolades ({}) and separated by commas (,). The element of a list, the item, can be identified by its relative position within the list. An empty list can declared simply by an empty set of accolades ({})

    Quote
    set aList to {1, true, "Three", {4}, 5.0}


  • Number:  A number a is a numerical value, positive or negative that can be either an Integer or a Real:
    • Integer: Number, positive or negative that does not contains a fractional part. Ex. –1000, 2, 23400.
    • Real: Number, positive or negative that can contain fractional part. Ex. –-24.5, 75.234, 3.14159265359.
  • Record: A Record is an unordered series of values of any type bound together by accolades ({}) and separated by commas (,). The element of a record must have a unique label and must be assigned a value

    Quote
    set aRecord to {FirstName:"Michel", LastName:"Lemieux", NickName:"Publi-Script", age:40}


  • String (or Text): A String is an ordered series of characters bound together by quotation marks (“”).
    Note: A text and a string object are equivalent but the class of a text or a string will always be a string.

    Quote
    set aString to "This is a String"
    set aText to aString as text
    class of aText
    -->     string


Coercing values

In the course of your scripting life, you will find yourself wanting for a certain variable to a similar value but in a different data type.
For instance the string "2" and the integer 2 while similar will not support the same commands.

Applescript propose two different ways to converting (or coercing) a data type int another:
  • The use of the 'as" command: You have probably seen my use of this technique in the past. The "as" command makes YOU the boss.

    Quote
    set myNum to 534
    —>     534
    set myText to myNum as text
    —>     “534”
    set myReal to myText as real
    —>     534.0
  • The other technique is transparent. In some case, when AppleScript expect a certain data type and is presented with an other, it will try to coerce it silently to the desired data type.

    Quote
    set Total to "234" + 24
    —>     258

    set TotalText to "Total: " & Total
    —>     “Total: 258”

Before you think you are out of the woods and that coercion will solve all your problems, I must tell you that not all data type can be coerced to every other. The table on the right outlines the coercion capabilities of the most commonly used data types.



Of course, the actual value of the data type you wish to coerce plays a big role in the process. For instance, the preceding table states that a string can be coerce into an integer but you would be hard pressed trying to get an integer out of the word "house".

Quote
set aString to "Two"
set aInteger to aString as integer
—>     Can't make "Two" into type integer.

set bString to "2"
set bInteger to bString as integer
—>     2
           
           



« Last Edit: October 22, 2008, 07:45:52 AM 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

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Lesson #2.4 : Data Types
« Reply #1 on: June 13, 2007, 10:55:28 AM »
I realized I forgot to include the "coercion" information. I rectified the omission.
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