Using launchd, you could saveSave a property list like this inas ~/Library/LaunchAgents/test.plist
.:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.stackexchange.apple.40705<<string>test</string>
<key>ProgramArguments</key>
<array>
<string>say</string>
<string>yy</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Users/username<string>~/Desktop/</string>
</array>
</dict>
</plist>
The property list is loaded automatically after logging out and back in, but youagent can load it immediatelybe loaded with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705test.plist
or by logging out and back in. Applying changes requires unloading and loading the property listplist.
Programs can only be run at most every ten seconds. You can increaseTilde expansion (but not decrease~/
) the time limitworks in WatchPaths by addingdefault. EnableGlobbing adds wildcard and tilde expansion for ProgramArguments, but it doesn't affect Program or WatchPaths.
If a watched file is modified within ten seconds from the last invocation, a message like this is added to system.log:
com.apple.launchd.peruser.501[146]: (test) Throttling respawn: Will start in 7 seconds
One way to get rid of the messages is to add something like ThrottleIntervalsleep 10
keyto the end of scripts. Setting ThrottleInterval to 10 doesn't help.
Changes in subfolders of watched folders aren't detected.
See man launchd
and man launchd.plist
for more information.