Variables

    A variable stores a value so that you can use it in expressions. You use variables, rather than literal values, for several reasons:
    • You may not know the literal value when you write your script.
    For example, if your script changes the name of a file, it will need to use the existing name of the file. Obviously, you can't put the name of every file into your script.
    • Variables are easier to work with than some actual values.
    For example, if the actual value is a string of characters, using a variable makes it easier to use the value several times in the script.

    To designate a variable, you use a name. Variable names can be anything you want. They are not case-sensitive, so "theName" is the same as "thename."

    To create a variable, you assign it a value:

    set theWord to "Mark"

    In the example, "theWord" is the variable. After the command is executed, the variable contains the value "Mark."

    You can also use an expression to assign a value to a variable:

    set theWord to word 1 of text body of document "Simple"

    To use a variable, include its name in a command or another expression.

    display dialog theWord

AppleScript defined variables

    AppleScript defines two special variables:
    • it is a reference to the default target
    • me or my is a reference to the current script

 


Table of contents