One of the hidden gems in OS X is a scripting service called Folder Actions, where you can bind an AppleScript to run whenever the contents of a specified folder are changed. This feature can be exceptionally useful, not only for helping you organize files and folders, but also for helping you monitor aspects of your system.
Unfortunately Apple does not promote Folder Actions, and perhaps they might someday go the wayside and be gone from OS X, but for now while they are still around you might be able to make good use of them.
Check this box in the AppleScript Editor’s preferences to show the AppleScript menu (click for larger view).
To enable folder actions, you will need to activate the script menu from the AppleScript editor utility:
- Open AppleScript Editor (or “Script Editor”) in the Applications > Utilities folder
- Go to this program’s preferences
- Check the box in the General section to “Show Script menu in menu bar.”
The menu that appears on the left-hand side of your menu bar should look like a little scroll, in which you can choose “Configure Folder Actions” from the “Folder Actions” menu. This will open a small panel, where on the left you can add watch folders to the list, and then select each and bind separate scripts to them by clicking the plus button under the column on the right.
Apple includes several built-in scripts, one of the more relevant ones being the “add — new items alert” script, which will warn you when new items have been placed in the watched folder. This can be useful for some everyday tasks like watching a shared dropbox for new items, but it can also be useful for watching vulnerable system folders to help ward off malicious files being added to them.
A chosen folder to the left, can have multiple scripts bound to it in the right column. In this case, watching various Launch Agent folders on the system can help prevent malware from installing unwanted launcher scripts (or at least notify you when new launcher scripts are added (click for larger view).
Folder Action scripts are simply AppleScripts, so if you are familiar with Apple’s scripting language then you can create some fairly complex handling routines for your watched folders; however, if not then you can still make use of Folder Actions with Automator, Apple’s simplified workflow-based scripting routine for OS X:
- Open Automator and press Command-N to create a new workflow
- Select “Folder Action” as the type of workflow
- Choose the desired folder to watch from the top of the workflow
- Add the various actions you desire to the workflow, to create your scripted routine.
- Save the workflow and exit.
By choosing “Folder Action” as the Automator workflow type, the saved file will be stored in the username > Library > Workflows > Applications > Folder Actions directory. You will then be able to change which folders this script is bound to in the Folder Actions Setup utility.
By starting with Automator, you can implement some fairly robust scripts to bind to folders, and when you need more versatility, you can then migrate to performing the same actions and more in AppleScripts.