Folder Action Scripts
Folder actions are one of the most popular features of AppleScript. They are implemented differently on Mac OS X than they were in Mac OS 9, but that rarely concerns you; their purpose is the same.
When scripts are attached to folders, they run automatically when certain events occur. Three standard folder actions are supplied in Mac OS X, and you can add others.
The first folder action lets you know when items have been added to a folder. You receive the notification shown in Figure 3.4, and you have the option to open the folder and see the items highlighted.
Figure 3.4 A script can notify you of changes to a folder.
If you're used to working alone at your computer using the keyboard and mouse to control everything that happens, you might wonder why this would matter. But if you're on a network and you allow people to put files into your Public folder, attaching an add action to that folder will keep you updated about their updates about which you would otherwise know nothing.
This is also a useful action to have if you have a background application or script running that will present its results in a file. You can see that it's finished and deal with it whenever you want to.
Another folder action lets you automatically close subfolders when the main folder is closed. If you have folders such as ToDos, Invoices, and Meetings inside your Documents folder, all of them would be closed when you close your Documents folder if you attach such an action to Documents.
Finally, you can use a folder action to display a comment when the folder is opened. Figure 3.5 shows a comment that you can type into the Info box.
Figure 3.5 You can type a comment into a folder's Info box.
If you attach an open folder action to that folder, the comment will be displayed whenever the folder is opened, as you can see in Figure 3.6.
Figure 3.6 A folder action can show the comment in a dialog when the folder is opened.
To use folder actions at all, you need to enable them on your computer. From Script Menu, use Enable (or Disable) Folder Actions to turn them on and off.
To attach folder actions to a specific folder, use Attach Folder Action. To remove one, use Remove Folder Action. The convenience script, Edit Folder Action, opens the selected folder's action in Script Editor.
You can create your own folder actions to be added to folders by placing them in the Folder Action Scripts subfolder inside either of the two Scripts folders (/Library/Scripts and ~/Library/Scripts).
When you write your own folder actions, you should know that in addition to the three actions that can trigger scripts described previously (open, close, and add), there are two others. You can be notified if items are removed from a folder; you can also be notified if a folder window is moved.