Chapter Review

Congratulations, you’ve finished your initial foray into the world of AppleScript. You’ve learned what AppleScript is, how it works, and how to script the Finder application to control the display of Finder windows.

In addition to creating a useful script tool, you’ve learned these important AppleScript concepts:

  • On the computer, everything is an object.
  • On the computer, everything belongs to, is related to, is contained in, or is part of something else. For example, the Finder application contains windows and folders.
  • Scriptable objects are referred to by their positions in their object hierarchy or where they are in their “chain of command.” For example:
    file "cars.pdf" of folder "Documents" of folder "sal" of folder "Users" of startup disk
  • Scriptable objects have properties with values that can be read and sometimes manipulated. For example, a Finder window has name, position, and bounds properties.
  • The AppleScript verb get is used to retrieve the value of a property, as in:
    get the name of the front window
  • The AppleScript verb set is used to alter the value of editable properties, as in:
    set the position of the front window to {0, 44}
  • Scripts are written using single-line tell statements or multiple-line tell blocks that target one or more actions at a scriptable object.
  • Textual content used in a script is placed within straight quotation marks ( " " ).
  • Lists in AppleScript begin with the opening brace character ( { ), contain items separated by commas, and end with a closing brace character ( } ).

Finder Window Properties

For reference, the following is a summary of the Finder window properties and commands used to create the Desktop Setup script. Review these items, keeping in mind that the principles you’ve learned here apply to all scriptable applications, not just the Finder.

Read-Only Properties

Read-only properties are properties whose corresponding values can be accessed or read only; they cannot be changed.

name: the text displayed in the title bar of the window
tell application "Finder" to get the name of the front window

Editable Properties

Editable properties are properties whose corresponding values can be both read and changed.

index: an integer (whole number) indicating the position of the window in the stack of open windows
tell application "Finder" to get the index of the front window

bounds: a list of coordinates defining the top-left and bottom-right corners of a window
tell application "Finder" to set the bounds of Finder window 1 to {0, 44, 400, 300}

collapsed: a true or false value indicating whether the window has been minimized to the Dock
tell application "Finder" toset collapsed of every window to false

current view: the method currently used to display content in the window. Values for this property are one of the following: icon view, list view, column view, and, in Leopard, flow view
tell application "Finder" to set the current view of Finder window 1 to icon view

position: a list of coordinates indicating the horizontal and vertical offset of the window from the left and top sides of the screen
tell application "Finder" to set the position of the front Finder window to {0, 22}

target: a reference to the folder or directory whose contents are displayed in the window.
tell application "Finder" to set the target of the front Finder window to home

toolbar visible: (Mac OS X 10.3) a true or false value that indicates whether the Finder window displays a toolbar.
tell application "Finder" to set toolbar visible of the front Finder window to false

statusbar visible: (Mac OS X 10.4) a true or false value that indicates whether the Finder window displays a status bar. This works only when the toolbar is not visible.
tell application "Finder" to set statusbar visible of the front Finder window to false

sidebar width: (Mac OS X 10.4) an integer value that indicates the width of the Finder window’s sidebar in pixels. To close the sidebar in Mac OS X 10.4 (Tiger) without hiding the toolbar or the status bar, assign this property a value of 0. In Leopard, setting the sidebar width property to 0 reduces it to a width of 135 pixels.
tell application "Finder" to set the sidebar width of the front Finder window to 0

zoomed: a true or false value that indicates whether the window has been expanded.
tell application "Finder" to set zoomed of the front Finder window to true

Referring to a Finder Window

Finder windows, and other scriptable objects as well, can be referenced in a variety of ways:

by name:
Finder window "Documents"
by numeric index:
Finder window 1
by descriptive index:
the first Finder window
the second Finder window
the fifth Finder window
the 1st Finder window
the 23rd Finder window
by By position relative to other windows:
the front Finder window
the middle Finder window
the back Finder window
the last Finder window
the Finder window before the last Finder window
the Finder window after the first Finder window
by random index:
some Finder window

Verbs Used with Finder Windows

AppleScript verbs are the commands targeted at scriptable objects to retrieve or manipulate property values and to perform actions with the scriptable objects.

Accessing the Values of a Window’s Property

get: used to access the current values of a window property.
tell application "Finder" to get the target of the front Finder window

Changing the Values of a Window’s Property

set: used to apply a new value to a window property.
tell application "Finder" to set the target of the front Finder window to home

Controlling Windows

open: causes a window to become visible in the Finder
tell application "Finder" to open the Finder window of the startup disk
tell application "Finder" to open the startup disk

close: causes a window to close
tell application "Finder" to close every Finder window

select: causes a window to come to the front
tell application "Finder" to select the last Finder window

The Tell Statement

A tell statement is a single AppleScript statement, beginning with the verb tell, that contains a reference to the target object and the action to be performed:

tell application "Finder" to set the target of the last Finder window to home

The target object in this example is the last Finder window displayed on the Desktop. The action to be performed is to change the contents of the window by altering the value of the window’s target property to be the user’s home directory.

The Tell Block

Use tell blocks to replace multiple tell statements and target multiple actions at a scriptable object or objects. All tell blocks begin with the verb tell followed by a reference to a target object or parent application. Statements defining the actions to be performed are placed after the opening line, with each statement getting its own line. The tell block is closed with the statement end tell.

tell application "Finder"
 set the target of the front Finder window to home
 set the current view of the front Finder window to icon
 set the position of the front Finder window to {0, 22}
end tell

tell blocks within other tell blocks are called nested tell blocks. When compiled, the hierarchy of the scriptable objects is revealed by the indentation applied to the formatted script text.

tell application "Finder"
 tell the front Finder window
 set the target to home
 set the current view to icon
 set the position to {0, 22}
 end tell
end tell

TOP | CONTINUE