[ Team LiB ] |
24.3 Folder ActionsA folder action is a behavior that occurs automatically when certain events take place in a designated folder in the Finder. Folder actions are not implemented as applications, but as scripts (but they seem to have found their way into this chapter anyway). As with digital hub scripting, your script has certain handlers which, if present, will be called when the corresponding event takes place. Any scripts to be used as folder action scripts should live in ~/Library/Scripts/Folder Action Scripts. Alternatively they can live in /Library/Scripts/Folder Action Scripts, and some example scripts are located there; but the former, the one in your user library, is the default. Setting up a folder action involves the association of a script with a folder. To make such an association is to attach the script to the folder; to break the association is to remove the script from the folder. The script is not actually moved; attachment and removal are conceptual, not physical, and are performed through System Events (see Section 21.2.3), which ties a folder and a script together through a folder action object. It is up to System Events to maintain these folder action objects and to respond correctly when an appropriate action in the Finder takes place. So System Events must be running in order for folder actions to work; that is why, when you enable the folder actions mechanism, System Events is added to your Startup Items. In general, you don't have to worry yourself with the details of scripting System Events to manipulate folder actions. Apple provides a straightforward user interface through the application Configure Folder Actions, located in /Applications/AppleScript. Here you can turn the folder actions mechanism as a whole off and on; add and remove folders to the list of those that have folder actions; attach and remove scripts in association with any listed folder; enable and disable a particular folder or script; and open a script for editing. Another interface is provided by a folder's contextual menu. 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 so you're only manipulating scripts in connection with this one folder. You can attach a script to, and remove a script from, this particular folder, as well as edit an attached script. You can also enable or disable the folder actions mechanism, and open the Configure Folder Actions application. There are also some scripts located in /Library/Scripts/Folder Actions, which may be accessed directly or through the Script Menu (see Section 2.4). These are useful for study if you want to learn how to drive the folder actions mechanism by scripting System Events. Here is what they do:
The latter two are especially interesting; look in particular at the use of the commands attach action to, attached scripts, and remove action from. So much for how to associate a script with a folder. What about the code that goes into a folder action script? The terminology for the events to which a folder action script can respond is defined in the dictionary of the StandardAdditions scripting addition. Therefore the parameter in the first line of your handler definition doesn't have to be in parentheses. A folder action script can respond when a folder's window is opened, moved, or closed, or when items are added to or removed from the folder. An example appears at Section 2.5. There are a number of educational examples in /Library/Scripts/Folder Action Scripts as well. |
[ Team LiB ] |