beautypg.com

Writing scripts that control the finder 1 – Apple AppleScript Finder Guide User Manual

Page 22

background image

C H A P T E R 1

Introduction to Finder Scripting

10

Writing Scripts That Control the Finder

Writing Scripts That Control the Finder

1

You can use a variety of techniques in scripts you write yourself that you can’t

use in recorded scripts. For example, suppose you have several different

window arrangements that you like to use, and they tend to evolve and

change. Instead of recording a series of scripts, you can write a single script

that allows you to perform one of two actions: take a snapshot of the current

window arrangement or restore the most recently taken snapshot.
To write a script that performs these tasks, you need to know something about

script properties, variables, the Display Dialog scripting addition, If statements,

and Repeat statements. For more information about these AppleScript features,

see the AppleScript Language Guide and the AppleScript Scripting Additions Guide.
Listing 1-2 shows an example of such a script. It begins by declaring two script

properties, boundsAll and refsAll, that hold the windows’ bounds and

references to the objects the windows belong to when the script takes a

snapshot. At first these properties consist of empty lists. When the script takes

a snapshot of the current windows, these properties “remember” the windows’

bounds and references until a new snapshot is taken or the script is recompiled.
After declaring its properties, the script uses the Display Dialog scripting

addition command to display a dialog box. The dialog box asks the user to

make a choice and displays two buttons: Restore Old Snapshot or Take New

Snapshot. These buttons control which of two parts of the If statement that

follows runs next.
If the user chooses Take New Snapshot, the second part of the If statement

(beginning with else) takes control and stores the bounds and references

for the open windows in the boundsAll and refsAll properties. If the user

chooses Restore Old Snapshot, the first part of the If statement closes all the

windows that are currently open and uses the values stored in the properties to

identify the items whose windows are to be opened, open their windows, and

set the windows’ bounds. These actions are accomplished with a Repeat loop

that acts on one item and its window at a time.