views:

63

answers:

3

Hi

I want to write a little script on my Mac. This script will basically look for new files in a particular folder, and move them to another location when some are found.

So I was about to write something very basic, with an infinite loop, and I was wondering if something a bit more nice already exist ? Like a Listener or something I could use ?

Thanks !

+6  A: 

You want to look into Folder Actions

http://www.simplehelp.net/2007/01/30/folder-actions-for-os-x-explained-with-real-world-examples/

Lou Franco
+2  A: 

An alternative way, slightly more low-level than folder actions, but I suspect more flexible, is to use launchd to watch a folder.

See launchd.plist(5), or the overview documentation for launchd (unfortunately, this overview documentation is primarily concerned with daemons, but the principle is the same; the key you're interested in is WatchPaths, so searching for that might find something more like a tutorial).

If you go this route, you need to create a .plist like the following, which runs the command /path/to/virus/scanner.sh /Junk/Downloads whenever the /Junk/Downloads directory is modified.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>localhost.clamav.clamscan</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>Nice</key>
    <integer>1</integer>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/virus/scanner.sh</string>
        <string>/Junk/Downloads</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Junk/Downloads</string>
    </array>
</dict>
</plist>

Put that in $HOME/Library/LaunchAgents/foo.plist, and the command launchctl load $HOME/Library/LaunchAgents/foo.plist will start it going.

Norman Gray
+1  A: 

Thanks Lou.

I had a look at your link and figured out how to create my own folder action doing what I want.

Just sharing the action in case someone want to use it : MoveAction

It works fine for now !

Farid