There are many good reasons for wanting to keep watch on specific folders. Your workflow might generate images into a designated location which then have to be passed through another process. You might want to copy documents into a folder which then ensures that they are moved to an external archive, or synchronised with remote storage.
Some folders are of particular significance too: changes made to Library/LaunchAgents and LaunchDaemons folders can be early signs of malware taking up residence, for instance, and good anti-malware tools such as those from Objective-See, and Sqwarq’s DetectX can keep an eye out for you.
Watched folders can be invaluable when trying to diagnose or solve a problem too. If you aren’t sure which preferences file a particular setting is saved to, change that setting and watch which gets updated in ~/Library/Preferences. I have heard of users who see received email messages briefly before they vanish: it would be so helpful then to be able to watch the message arrive in their mailbox folder, and see what happens to it.
One simple approach to this might be to have a switch for each folder which you could turn on to send records of changes in that folder to your Mac’s log. Not only is there no such switch (as far as I know), but the unified log in Sierra and High Sierra is not a good place to record such information anyway.
The traditional tool built into macOS to handle many of these needs is the AppleScript Folder Action.
This is straightforward if you can express what you want to do with files in your watched folder in terms of an AppleScript. It is therefore most useful in workflows, and of least value in keeping a watch on changes in busy folders like ~/Library/Preferences, although an advanced scripter might still be able to make use of it.
Before you use Folder Actions, it is best to enable the AppleScript menu in Script Editor‘s preferences, under the General tab. This gives you easy access to scripts, including those used to enable and attach Folder Actions. A Script icon will then appear towards the right end of the menu bar, and its contents will change according to the frontmost app.
Bring the Finder to the front, and select the Enable Folder Actions item from the Folder Actions command in the Script menu, and a one-line script containing
tell application "System Events" to set folder actions enabled to true
will be run to enable Folder Actions. In versions of macOS before Sierra, this used to be set using the Configure Folder Actions utility, from which you could also attach them to folders.
Instead, in Sierra and later you should be able to select the Attach Script to Folder command, select the Folder Action script, then the folder to be watched. This should enable you to watch a folder for the addition of new items, for example, and either notify you in an alert, or perform a scripted action using an app.
As with much of AppleScript, Folder Actions are ageing, and increasingly feel as if they are rickety old parts of macOS which don’t work as well or as reliably as they did in the past. If you’re already comfortable with AppleScript and your task fits well with what you can achieve using it, Folder Actions can be valuable.
I wouldn’t recommend them for more demanding situations, such as monitoring large and busy folders, or for security purposes, and they’re not worth learning AppleScript for, any more.
This System Preferences pane by noodlesoft, costing $32, offers a wide range of folder actions which can be triggered by custom rules. It makes it consummately easy, for example, to watch many folders for added and changed files, to post notifications for some, and to record all the changes in its own traditional log file.
Its features also go far beyond simply watching folders. You can use it to sync folders, move or remove files on the basis of their age, automatically empty the Trash, and do a great deal more besides. Hazel has been going a long time – over ten years now – and is thoroughly mature and extremely well-supported.
I will be looking in more detail at some of the tasks you can accomplish using Hazel in future articles.
If you want a command tool to use in your own shell scripts, then look at Enrico Maria Crisostomo’s
fswatch, from here or via
brew. This supports two different forms of monitoring on macOS: the File System Events (FSEvents) system, and BSD’s kqueue. However, its author recommends that only the former is used on macOS, because the kqueue mechanism can cause the system to reach its maximum number of file descriptors.
It is also not entirely straighforward to use. The Mad Coder’s Blog, for example, recommends using a script such as
fswatch -0 $@ | while read -d "" event; \
which is called with the path to the folder to be monitored as its single parameter.
In the next article, I will look at macOS support for file and folder monitoring, and explain the three quite different mechanisms available: FSEvents, kqueue, and Grand Central Dispatch.