Team LiB   Previous Section   Next Section

6.2 Constants and Predefined Variables

Constants are reserved words that AppleScript has given a predefined value that you cannot change. There are boolean constants (true or false), date constants (e.g., April, May), and considering or ignoring constants (e.g., case, white space), among others. Predefined variables, on the other hand, have a changeable value. In other words, you can use code such as set pi to 5 and the pi predefined variable will no longer have the value of about 3.14159 in your script! You cannot change the value of the boolean constant false, however (set false to 3 will not compile). The constants are listed in Table 6-1, and the Predefined Variables are listed in Table 6-2. Certain date and time values (i.e., minutes, hours, days, weeks) are changeable in a script like predefined values; however, they are grouped with other date constants for convenience.

Table 6-1. AppleScript Constants

all caps

all lowercase

application responses

ask

bold

case

condensed

current application

date and time constants (e.g., January, February)

diacriticals

expanded

expansion

false

hidden

hyphens

italic

no

outline

plain

punctuation

shadow

small caps

strikethrough

subscript

superscript

true

underline

white space

yes

 

Table 6-2. AppleScript Predefined Variables
anything
it
me
missing value
pi
result
return
space
tab
version
my

all caps

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

all caps is a text-style constant that represents all capital letters. Some applications return text values that encapsulate the style information, such as font and point size, instead of leaving it as plain ASCII text. The syntax example shows the return value from an application command that gets the style of a chunk of text. The return value is of type record and contains the list of text-style constants that are on or off for the text. The AppleWorks command is:

get style of text body of document 1

Along with all caps, the text-style constants (italic, underline, outline) are reproduced as literal words without quotation marks.

all lowercase

Syntax

{class:text style info, on styles:{plain, all lowercase}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

This is a text-style constant that represents all lowercase text characters. Some Mac applications return text that encapsulates one or more of these text-style characteristics (e.g., italic, bold, underline). See all caps for an expanded description of text-style constants.

anything

Syntax

set myBasket to anything

Description

anything is a predefined variable of type class that can incorporate other classes. In other words, if a command's parameter type is anything then the command will accept more than one value type, as opposed to a command like display dialog that only takes one data type as its direct parameter, a string. The AppleScript Language Guide recommends the following usage of anything as an example. If you want a script to monitor a variable to determine if the variable's value or data type has changed, you can set a variable to anything. Then later on in the script test the variable to determine if its value has changed, as in set bool to (myBasket is equal to anything). If the bool variable is true, then the myBasket variable has not been changed, as in set to a string.

application responses

Syntax

Ignoring application responses

   (* do some scripting here *)

end ignoring

Description

The constant application responses can be included in an ignoring...end ignoring statement block to ignore an Apple event response from a program. This ignoring statement would usually take place in a repeat loop that is querying several different running programs for some purpose. Chapter 7, discusses ignoring and repeat.

Examples

This code tells the Finder to send the script a list of running processes on the computer. process is an element of the Finder application (check the application class in Finder's dictionary and you will find "process" listed under "elements"). The code every process returns a list of process objects, which are otherwise known as running applications on the computer:

tell application "Finder"

   (* procs contains a list of running processes on the computer *)

   set procs to every process

   repeat with p from 1 to 4 (* do something with the first four listed 

   processes *)

      ignoring application responses -- ignore any reply Apple events

         (*script will try to get name of each process but ignore the 

         response, and 'myname' will not get a value *)

         set myname to (name of (item p of procs))

         try

            tell application myname

               display dialog myname

            end tell

         end try

      end ignoring

   end repeat

end tell

If you look at Script Editor's Event Log while this program is running, you'll see a sequence of Apple events sent to each process to retrieve its name (i.e., get name of process "Folder Actions"), but the script will ignore any of the application responses sending the app's name. As a result, the myname variable will not be set to a valid value. Consequently, the tell application myname statement will raise an error (handled by the try...end try block), and no dialogs will ever be displayed.

Error handling with try blocks is covered in detail by Chapter 7.

If you substitute considering...end considering for ignoring...end ignoring (considering application responses is the default behavior for scripts, so you do not actually have to include it), then the script will do what it's supposed to. It displays four dialogs one after the other containing four of the running process's names.

ask

Syntax

Close document 1 saving ask

Description

ask is a constant parameter to the close command or Apple event, as in close document 1 saving ask. If this parameter is included, in lieu of its alternatives no or yes, then the closing application must produce a dialog asking the user whether to save the document before closing it.

Examples

This AppleScript shows how to use this command. If you use no then the document is closed, whether or not any unsaved changes have been made to it. If you use yes then the app closes the document and saves it using its present filename:

tell application "BBEdit 5.0"

   activate

   close document 1 saving ask (* other options are 'saving no' and 

   'saving yes' *)

end tell
bold

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{bold, italic, underline, outline, shadow, condensed, expanded, 

strikethrough, superscript, subscript, superior, inferior, double underline}}

Description

bold is a text-style constant that represents a boldface rather than plain character. Some applications return text values that encapsulate the style information, such as font and point size, instead of leaving it as plain ASCII text. See all caps in this section for a further discussion.

case

Syntax

Considering case

   "Colorado" is equal to "colorado" -- returns false

end considering

Description

case can be used in a considering...end considering statement block to take into account upper- or lowercase when making a string comparison. AppleScript does not consider case in string comparisons by default. The considering case statement block will consider character case in all the statements within its block.

Examples

The following program will run silently, because the boolean expression "animal" is equal to "aNiMal" returns false considering case:

considering case

   if "animal" is equal to "aNiMal" then beep 1

end considering
condensed

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

superscript, subscript, superior, inferior, double underline}}

Description

condensed is a text-style constant that represents the condensed style in a text-editing application such as AppleWorks. The condensed style reduces the space between words. Some applications return text values that encapsulate the style information, such as font or point size, instead of leaving it as plain ASCII text. See all caps in this section for a further discussion.

current application

Syntax

set ap to current application

Description

The current application is the default application that receives script commands, in the absence of explicit tell statements. Chapter 7discusses the tell statement. You can set the default application for an entire script by declaring a parent property:

property parent : application "Finder"

This statement establishes the Finder as the target of any script commands that are not located in a tell statement.

Examples

The upcoming example uses some Finder commands (the Finder is the default or current application). Then the script sends activate, set, and get Apple events to the text editor BBEdit. The last two lines show that you can use the current application constant to set a variable to the script's default target application. The last line:

tell ap to get my name

returns "Finder." my is a built-in AppleScript reference to the script object itself, whose parent is the Finder. my name returns "Finder" (Chapter 8 discusses script objects):

property parent : application "Finder"

open file "events.html"

get name -- returns "Finder"

tell application "BBEdit 5.0"

   (* BBEdit receives activate, set, and get Apple events *)

   activate

   set mytxt to (get line 1 of document 1)

end tell

set ap to current application

tell ap to get my name
date and time constants

Syntax

Months

January, February, March, April, May, June, July, August, September, October, November, December

Days

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

time constants

minutes, hours, days, weeks

Description

The month constants are returned by getting the month property of a date object, as in get month of mydate. You can also use them to test which month is represented by a certain date object:

set mydate to date "Friday, January 1, 2000 12:00:00 AM"

if day of mydate is 1 and month of mydate is January then display dialog 

"Happy New Year!"

The day constants are the day-related values that are returned by a date object's weekday property.

These constants, normally used with dates, represent the following values:

minutes

60

hours

60 * minutes

days

24 * hours

weeks

7 * days

Examples

To calculate how many minutes are in a day (1440), you could write:

set dayMinutes to days / minutes

These constants contain integer value types. A common way to use them is to add or subtract time from date objects:

set newdate to (date "January 1, 2000") + (20 * weeks)

This example adds 20 weeks to the first day of the last year of the millennium (yes, I'm strictly Gregorian in my view of which year represents the last one of the millennium). If you wanted to, you could use these reserved words as integer constants. For example, minutes returns 60 and weeks returns 604800 (or, 7 * 24 * 60 * 60). You have to be exact in your usage of these constants, if not in your grammar. If you want to add one week to a date, for instance, you have to use code similar to the following:

mydate + 1 * weeks

You cannot write:

mydate + 1 week
diacriticals

Syntax

Ignoring diacriticals

   (* do some scripting here *)

end ignoring

Description

These accent characters are ignored in string comparisons that use an ignoring diacriticals...end ignoring statement block. By default, AppleScript considers diacriticals when comparing strings.

Examples

This AppleScript shows that by default the strings that have diacritical marks do not return true when compared with the same string without the marks. The result is reversed when the diacritical constant is ignored (hasDiacritical returns false and hasDiacritical2 returns true):


set hasDiacritical to ("spät is late in German" is equal to "spat is late 

in German") -- returns false

ignoring diacriticals

   set hasDiacritical2 to ("spät is late in German" is equal to "spat is 

   late in German") -- returns true

end ignoring

log hasDiacritical -- is false

log hasDiacritical2 -- is true because the diacritical in "ä" is ignored
expanded

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

expanded is a text-style constant that represents the expanded style in a text-editing application such as AppleWorks. The expanded style increases the space between words in a sentence. (Some applications return text values that encapsulate the style information, such as font or point size, instead of leaving it as plain ASCII text. See all caps in this section for a further discussion.)

expansion

Syntax

Ignoring expansion

   (* include string comparison here *)

end ignoring

Description

If expansion is included in strings that are located in an ignoring expansion...end ignoring statement block, then AppleScript considers the characters Æ, æ, Œ lig;, and œ lig; to be single characters and not equal to AE, ae, OE, and oe. By default, AppleScript considers the string "Æ" to be equal to the string "AE." Chapter 7 is devoted to AppleScript statements such as ignoring...end ignoring.

false

Syntax

Set falseBool to false

Description

false is the boolean constant that equals false or not true. Chapter 3 discusses the boolean data type.

hidden

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{bold, italic, underline, outline, shadow, condensed, expanded, hidden,

strikethrough, superscript, subscript, superior, inferior, double underline}}

Description

hidden is a text-style constant that represents the hidden style in a text-editing application such as AppleWorks. Some applications return text values that encapsulate the style information, such as font or point size, instead of leaving it as plain ASCII text. See all caps in this section for a further discussion.

hyphens

Syntax

Ignoring hyphens

   "A hyphen" is equal to "A-hyphen"

end ignoring

Description

AppleScript considers hyphens in strings by default during string comparisons. You can change this behavior by enclosing the string-comparison statement in an ignoring hyphens...end ignoring statement block. This code shows that hyphens will not be considered when comparing strings in this manner:

ignoring hyphens

   set theTruth to ("burning-hot pavement" is equal to "burninghot ¬

   pavement") -- theTruth is true

end ignoring
it

Syntax

Tell it to get its name

Description

it is shorthand for a script's default target. The following example returns the string "9.0," which is the Finder's version in Mac OS 9. The script's default target was previously set to the Finder by declaring the Finder as a parent property (see current application in this chapter or Chapter 3 for more information on parent properties). Therefore, it is a reference to the Finder:

property parent : application "Finder"

tell it to get its version (* returns the value of the Finder's version 

property *)
italic

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

italic is a text-style constant that represents the italic style in a text-editing application such as AppleWorks. The italic style places one or more characters in a string in italics. Some applications return text values that encapsulate the style information, such as font or point size, instead of leaving it as plain text. See all caps in this section for a further discussion.

me

Syntax

Set returnVal to MyScriptFunc(  ) of me

Description

me is used to specify a script-defined subroutine or property, to distinguish it from the properties or functions of other applications that the script uses. me is most often used in the form propertynameorfunctionnameof me. In the following code, the me predefined variable is used inside a tell...end tell statement block to identify the getMegabytes function as a script subroutine, not a Finder command. If you left out the of me part of getMegabytes(b) of me then the script would raise an error, because it would tell the Finder to call its getMegabytes handler, but the Finder doesn't define this command. In the absence of the me or my constants, all commands inside of a tell...end tell block are directed to the application targeted by the tell block or to scripting additions (Chapter 7discusses tell blocks). The script identifies getMegabytes as its own subroutine, which is an elegant way to call this useful routine anywhere you want in the script:

tell application "Finder"

   set b to largest free block -- returns free memory block as bytes

   set mb to getMegabytes(b) of me

   display dialog mb

end tell

(* subroutine definition; converts bytes to megabytes *)

on getMegabytes(byteVal)

   if (class of byteVal) is in {integer, real} then

      set megValue to (byteVal / 1024 / 1024)

      return megValue

   else

      return 0

   end if

end getMegabytes
missing value

Syntax

{"192.168.0.1", "0.0.0.0", missing value}

Description

missing value is a predefined variable that takes the place of a missing property value in return values that involve lists, for instance. The following code asks the Network Setup Scripting system app for the IP address of each network configuration on the computer. Since not all configurations use the TCP/IP protocol (some use the AppleTalk protocol), some of them are not associated with an IP address. The return value for this example looks like {"192.168.0.1", "0.0.0.0", missing value, "0.0.0.0", missing value, missing value, "0.0.0.0", missing value}, where missing value takes the place of the return value for the configurations that do not have an IP address:

tell application "Network Setup Scripting"

   open database

   get IP address of every configuration (* returns list of IP addresses 

   and missing values *)

   close database

end tell
my

Syntax

Set megValue to my getMegabytes(bytes)

Description

my is a reserved word that has the same effect as using the reserved words of me in a script (see me in this chapter). There is no difference between the two predefined variables; you can use whichever one appears more readable to you (my gets my vote). In the following example, you replace the statement:

set mb to getMegabytes(b) of me

with:

set mb to my getMegabytes(b)

This tells AppleScript that you are calling the script's getMegabytes subroutine, as opposed to an osax or custom application command. In other words, my denotes script ownership of a property or subroutine.

no

Syntax

Close document 1 saving no

Description

no is a constant parameter to the close command or Apple event:

close document 1 saving no

If this parameter is included, in lieu of its alternatives ask or yes, then the closing application closes the document without saving it, whether or not the document has been changed. See "ask" earlier in this chapter.

outline

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

outline is a text-style constant that represents characters that are outlined in black (or some other text color), and white in the middle, rather than appearing as plain text. Some applications return text values that encapsulate the style information, such as a font and point size, instead of leaving it as plain literal text. See all caps in this section for more discussion of text-style constants.

pi

Syntax

Return pi * (radius^2)

Description

pi is a predefined variable that returns a real value of about 3.14159. You can use this predefined variable in geometry calculations, as in the following example. This script has a getArea subroutine that returns the area of a circle when passed the circle's radius as an argument. Chapter 8 discusses how to create your own functions.

set circleArea to getArea(12)

on getArea(radius)

   if (class of radius) is in {integer, real} then (* check to make sure 

   parameter is a valid number *)

      return pi * (radius ^ 2) -- this calc returns the area of a circle

   else

      return 0

   end if

end getArea
plain

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

plain is a text-style constant that represents plain text. Some applications will return text values that encapsulate the style information, such as its font or point size, instead of leaving it as plain ASCII text. See all caps in this section for more discussion of text-style constants.

punctuation

Syntax

Ignoring punctuation but considering case

   (* do string comparison *)

end ignoring

Description

AppleScript considers punctuation marks such as commas and periods in strings by default when it compares strings for equality. If you enclose the string comparison in the statement ignoring punctuation...end ignoring, however, AppleScript will ignore the punctuation when evaluating a string comparison. punctuation comprises the following characters in AppleScript:

.
?
:
;
!
\
'
"
`

The next example uses the punctuation constant to ignore any commas in the numbers entered into a dialog box by a user. If the script did not use the ignoring punctuation statement, and the user entered a number with one or more commas (such as 1,000,000), then the statement:

if inputNum as real  1000

would raise an error. This is because by default the strings with nonnumber characters in them cannot be coerced to integers or reals (with the exception of strings like "1.0e + 4," which would evaluate to the real type in scientific notation—1.0e + 4). In the case of the following script, the punctuation marks can be ignored, allowing "1,000,000" to be coerced to a real data type (which would look like 1.0e + 6). Chapter 3 discusses the real type.


set inputNum to text returned of (display dialog "Enter a number larger¬

than 1,000" default answer "")

ignoring punctuation

   if inputNum as real  1000 then

      display dialog "Remember it has to be bigger than 1,000"

   else

      display dialog "Your number is: " & inputNum

   end if

end ignoring
result

Syntax

Set lastExpr to result

Description

result is a handy predefined variable that contains the value of the last statement evaluated in a script. You can set variables to result as in:

set finalVar to result

or use the following code to display the result:

display dialog result

If there is not a valid result from an expression, as in the top of the next example, then trying to get the value of result will raise an error. The bottom tell block in the following code stores the value of the last expression (i.e., 2 * 2) in the result predefined variable:

tell application "Finder"

   activate -- this Apple event doesn't return a value to AppleScript

   get result -- this statement raises an error

end tell

tell application "Finder"

   2 * 2

   get result -- returns 4

end tell
return

Syntax

Set theStr to "one line" & return & "another line"

Description

The return predefined variable represents a return character. You will use this predefined variable a lot with the concatenation character (&) in order to make strings more presentable:


"a long sentence divided into" & return & "two lines with the return 

character"

Make sure not to confuse the return statement with the return predefined variable. AppleScript also uses return as a statement inside of subroutines. If you use the return statement, the subroutine or function will return to the part of the script that called the function. The return character is a predefined variable while return, as used in functions, is a flow-control statement (see Chapter 7). This code illustrates the difference between the two:


set aString to "String with " & return & "one return character." 

(* use the return constant *)

run_it(aString) -- call the function

on run_it(str) -- function definition

   display dialog str¬

      buttons {"okay", "Big deal", "cancel"} default button 2

   (*  use return statement to return the value of the button the user 

clicked to dismiss the dialog box *)

   return (button returned in the result) (* return statement, not constant *)

end run_it
shadow

Syntax

{class:text style info, on styles:{plain, all caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

shadow is a text-style constant that represents the display of characters with a drop shadow behind them. Some applications will return text values that encapsulate this style information, such as font and other embellishments, instead of leaving it as literal plain text. See all caps in this section for more discussion of text-style constants.

small caps

Syntax

{class:text style info, on styles:{plain, small caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

small caps is a text-style constant that represents the display of characters with small capital letters. Some applications will return text values to an AppleScript that encapsulate this style information, such as font and other embellishments, instead of leaving it as literal plain text. See all caps in this section for more discussion of text-style constants.

space

Syntax

"string with" & space & " an extra space"

Description

space represents a space character in a string. You can use it when building your own strings:

"String with " & space & space & "two extra spaces."

You can also use the tab and return predefined variables to exert more control on string appearance.

strikethrough

Syntax

{class:text style info, on styles:{plain, small caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

strikethrough is a text-style constant that represents the display of characters with a line drawn through them, as though someone editing the document had crossed them out. Some applications will return text values to an AppleScript that encapsulate this style information, such as font and other embellishments, instead of leaving it as literal plain text. See all caps in this section more discussion of text-style constants.

subscript

Syntax

{class:text style info, on styles:{plain, small caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

subscript is a text-style constant that represents the display of characters with a lower-than-normal baseline, as in subscript. Some applications such as AppleWorks will return text values to an AppleScript that encapsulate this style information, such as font and other embellishments, instead of leaving it as literal plain text. See all caps in this section for more discussion of text-style constants.

superscript

Syntax

{class:text style info, on styles:{plain, small caps}, off 

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

superscript is a text-style constant that represents the display of characters with a higher-than-normal baseline, as in superscript. Some applications such as AppleWorks will return text values to a script that encapsulate this style information, such as font and other embellishments, instead of leaving it as literal plain text. See all caps in this section for more discussion of text-style constants.

tab

Syntax

"A string" & tab & tab & "with two tabs in it."

Description

tab is a predefined variable that places a tab in a string:

"Here is a string" & tab & tab & "with two tabs in the middle of it."

You can also insert return and space characters into your strings.

true

Syntax

Set boolVal to true

Description

true is the boolean constant that equals true or not false. Chapter 3 discusses the boolean value type.

underline

Syntax

{class:text style info, on styles:{plain, small caps}, off  

styles:{italic, underline, outline, shadow, condensed, expanded, strikethrough,

 superscript, subscript, superior, inferior, double underline}}

Description

underline is a text-style constant that represents the display of characters that are underlined, as in underline. Some applications will return text values to an AppleScript that encapsulate this style information, such as font and other embellishments, instead of leaving it as literal plain text. See all caps in this section for more discussion of text-style constants.

version

Syntax

Get version as string

Description

AppleScript's version property returns a version value (yes, version objects are based on the version class) that can be coerced to a string, as in "1.4." The number is the version of AppleScript that the machine running the script has installed. The following code shows how you can test for AppleScript's version. (Other applications, such as the Finder, also have a version property. Check the application class in the program's dictionary to find out if they do.)

set ASver to version as string

(* grab the "1.4" part of the value and coerce it to a number so that it can be

tested *)

if (text 1 thru 3 of ASver as real) 3 1.4 then

display dialog¬

"Good, you're running at least AppleScript version 1.4"

else

display dialog¬

"Perhaps you should consider upgrading to a newer AppleScript version"

end if
white space

Syntax

Ignoring white space

   (* do string comparison *)

end ignoring

Description

If you enclose a string comparison in an ignoring white space...end ignoring statement block, then AppleScript ignores any spaces, tabs, or return characters when comparing strings. Otherwise, AppleScript takes these characters into account during string comparisons. Ignoring white space might be useful when checking for certain content strings seized from textually complex sources like web pages. The following code shows what happens when you ignore white space:


set string1 to "a spread" & tab & tab & return & "out string."

set string2 to "aspreadoutstring."

ignoring white space

   return (string1 is equal to string2) -- returns true

end ignoring
yes

Syntax

Close document 1 saving yes

Description

yes is a constant parameter to the close command or Apple event:

close document 1 saving yes

If this parameter is included, in lieu of its alternatives ask or no, then the closing application saves the document, if it has been changed, before closing it. Depending on the application's default behavior, it usually prompts you for a filename (if the document does not have a valid filename) before closing it. Therefore, you should use the activate command to bring the target application to the foreground, so that the user can see any dialog box. See "ask" in this chapter for more info.

    Team LiB   Previous Section   Next Section