开发者

Be notified when a file is dropped into a folder Mac OS X

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 somethin开发者_运维问答g I could use ?

Thanks !


You want to look into Folder Actions

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


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">
<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.


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 :

Be notified when a file is dropped into a folder Mac OS X

It works fine for now !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜