beautypg.com

Apple AppleScript Finder Guide User Manual

Page 58

background image

C H A P T E R 2

Finder Objects

46

Object Class Definitions

EXAMPLES

The first example opens all the containers on the desktop except for the Trash.

tell application "Finder"

open (containers in desktop whose name is not "Trash")

end tell

The next example, if saved as a script application, toggles the value of the

Completely Expanded property of all containers at the top level of the front

window: that is, it alternately expands all the containers and their nested

containers or collapses them. The window in which the script application

is located must be open in list view for the script to work on the window’s

contents.

property expan : false

tell application "Finder"

if expan = false then

set completely expanded of containers in ¬

container of front window to true

set expan to true

else

set completely expanded of containers in ¬

container of front window to false

set expan to false

end if

end tell

The script begins by declaring the value of the expan property to be false.

This property is a Boolean value that changes after the script is run. The

script sets expan to true whenever it sets the Completely Expanded

property of the front window’s containers to true, or to false whenever

it sets their Completely Expanded property to false.
The value of the property container of front window is a reference to the

container to which the window belongs. This script must specify the Container

property of the window, rather than the window itself, or it may not work

consistently. A window’s container always has a fixed number of elements until

new elements are added or deleted, whereas a window open in list view has a

variable number of elements depending on which folders are expanded.