Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Finder Guide /
Chapter 1 - Introduction to Finder Scripting


Writing Scripts That Control the Finder

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.

Listing 1-2 A script that either takes a snapshot of the current window arrangement or restores a previously stored snapshot

property boundsAll : {}
property refsAll : {}

set dialogResult to display dialog �
   "Restore old snapshot or take new one?" �
   buttons {"Restore Old Snapshot", "Take New Snapshot"}
if button returned of dialogResult is "Restore Old Snapshot" then
   tell application "Finder"
      close windows
      set increment to 0
      repeat (number of items in refsAll) times
         set increment to increment + 1
         set i to item increment of refsAll
         open i
         set x to item increment of boundsAll
         set bounds of window 1 to x
      end repeat
   end tell
else
   tell application "Finder"
      set boundsAll to the bounds of every window
      set refsAll to the item of every window
   end tell
end if
To keep this script handy, store it as a script application on the desktop. Whenever you want to record or restore a window arrangement, double-click the application's icon. If you want to be able to switch back and forth between several window arrangements, duplicate the script and use each copy to create and store a different one.

Listing 1-3 shows another script that uses the Display Dialog command. This script automates a couple of tasks that network administrators often have to perform: adding a new user and adding a drop folder for that user.

Listing 1-3 A script that adds a new user to the Users & Groups control panel and adds a drop folder for that user to the startup disk

tell application "Finder"
   set dialogResult to display dialog �
      "Name of new user:" default answer ""
   set newUser to text returned of dialogResult
   set cPanel to control panel "Users & Groups" of �
      control panels folder
   open cPanel
   make user at cPanel with properties {name:newUser}

   set newFolder to make folder at startup disk �
      with properties {name:newUser}
   set owner of newFolder to newUser
   set group of newFolder to "Department Group"
   
   set see folders of group privileges of newFolder to true
   set see files of group privileges of newFolder to true
   set make changes of group privileges of newFolder to true
   set see folders of owner privileges of newFolder to true
   set see files of owner privileges of newFolder to true
   set make changes of owner privileges of newFolder to true
   set see folders of guest privileges of newFolder to false
   set see files of guest privileges of newFolder to false
   set make changes of guest privileges of newFolder to true
end tell
Unlike the script in Listing 1-2, the script in Listing 1-3 consists of a single Tell statement, and the Display Dialog command is invoked from within the
Tell statement. The dialog box asks for the name of the new user and stores it
in the variable newUser. The three statements that follow open the Users & Groups control panel and use the Make command to create a new user object with the name stored in newUser.

The rest of the Tell statement consists of a series of Set commands that create a new folder on the startup disk with the new user's name and set the folder's owner and group and its sharing privileges. The owner and members of the Department Group have all sharing privileges set to true. The guest privileges See Folders, See Files, and Make Changes are set to false, false, and true respectively, so guests can drop items into the folder but can't open it. These privileges are signaled to guests by a belt around the folder and a small black arrow above it.

For the script in Listing 1-3 to work correctly, file sharing must be turned on and the computer on which the script is running must have a group named Department Group. You can modify that name or other settings in the script
to suit your own purposes. You can also modify the script in a variety of other ways. For example, you can add an If statement that checks for the presence
of a particular group and if necessary creates the group before creating the
new user.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996