AppleScript_ The Definitive Guide - Matt Neuburg [225]
The relationship maintained by a folder action object is one-to-many: a folder action object has one folder path property, but it can have many script elements, meaning that more than one script is attached to that folder. Thus, the same folder can trigger multiple scripts. You might take advantage of this architecture, for example, to have different scripts respond when different kinds of event occur. Furthermore, a folder action object can be disabled (without turning off the folder actions mechanism), and a script attached to a folder action object can be disabled (without removing it from the folder action object, and without disabling the folder action object).
Warning
Nothing prevents you from setting more than one folder action object to point to the same folder, but this is useless, as all but the first folder action object will be ignored when an event occurs. I regard this as a gross flaw in the folder actions architecture.
Because System Events is a scriptable application, you can give it all appropriate instructions explicitly by means of AppleScript. In general, however, there is no need to do so. Apple provides a graphical interface through the Configure Folder Actions application, located in /Applications/AppleScript. (You can start up Configure Folder Actions directly, or launch it from a button in the AppleScript Utility application.) With the checkbox at the top of the window, you toggle on or off the folder actions mechanism as a whole. Beneath this are two lists. The list on the left is called "Folders with Actions," but it is not a list of folders; it is a list of folder action objects. So, using the list on the left, you maintain the list of folder actions: you can create a new folder action, destroy an existing one, and even rename a folder action (this does not rename the assocated folder, of course). The list on the right shows the scripts associated with the folder action object selected on the left; here you can attach or remove a script from a folder action object. You can also toggle the enablement of a folder action object or an attached script, and you can open a script for editing.
Another interface is provided by way of a folder's contextual menu within a Finder window. This is a much simpler interface than the Configure Folder Actions application, because it is clear from the outset what folder is in question—it's the one whose contextual menu you're using—and because only commands appropriate to the state of the folder actually appear in the menu. Thus, if the folder actions mechanism is turned off, the only thing you can do here is turn it on. If the folder actions mechanism is turned on, you can attach, remove, or edit an associated script. The only thing you can't do is toggle the enablement of an existing folder action object or attached script.
If, on the other hand, you want to configure folder actions directly by using AppleScript to drive System Events, it is not difficult to do. There are some scripts located in /Library/Scripts/Folder Actions (accessible through the Script Menu—see "Script Runner" in Chapter 2) that can serve as useful examples of common tasks:
Enable Folder Actions
Turns on the folder actions mechanism.
Disable Folder Actions
Turns off the folder actions mechanism.
Attach Script to Folder
Prompts the user for a script and folder and attaches the script to that folder.
Remove Folder Actions
Prompts the user for a folder and a script and removes that script from that folder so that it is no longer a folder action for it.
Warning
The Attach Script to Folder script has a major bug: can you spot it? When the user selects a folder to attach a script to, then if a folder action object with the same name as