Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

For example, I'd like to automatically run YUI Compressor whenever I edit a JavaScript source file to output a compressed version.

Ideally the process does not run in the background but in a dedicated shell, so I can easily terminate the process at the end of the day.

What would be the cleanest and most flexible way to watch specific files and/or directories for file changes and perform a custom action inside a shell?

share|improve this question

2 Answers 2

Watching a file for changes can be accomplished with a LaunchAgent. For example create a plist file at ~/Library/LaunchAgents/watch.and.lol.plist and fill it with this content:

<?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>watch.and.lol</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/Shared/changeAction.sh</string>
        <string>-force</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/private/var/radmind/client/.radmindOnDemand</string>
    </array>
</dict>
</plist>

Now make sure that the file /private/var/radmind/client/.radmindOnDemand does exist. Then load the launchagent with command $ launchctl load ~/Library/LaunchAgents/watch.and.lol.plist. As soon as the file ~/Library/LaunchAgents/watch.and.lol.plist does no longer exist, this launchagent job will be unloaded.

Now write some shell script that watches your file for changes, like:

chsum1=""

while [[ true ]]
do
    chsum2=`md5 /private/var/radmind/client/.radmindOnDemand`
    if [[ $chsum1 != $chsum2 ]] ; then           
        compile
        chsum1=`md5 /private/var/radmind/client/.radmindOnDemand`
    fi
    sleep 2
done

Note: this shell script needs some extra work to be done to make it efficient. Now it polls every 2 seconds (sleep 2). Better is to exit the script after the compile command has executed. That requires the storage of the output of the md5 hash command that can survive the exit and relauch of this shell script.

share|improve this answer
    
Thank you. I don't really fancy writing a launch agent everytime though, I'm looking for a solution flexible enough to add watchers and specify the file to watch and the command to run as an argument on the fly. –  Gerry Aug 10 '12 at 8:05

The $25 Codekit does watch folders and files for changes and does compress/minify/combine JavaScript and CSS files.

I know this is only a partial answer for your question, as it is not a generic watcher, but it does fit your example:

whenever I edit a JavaScript source file to output a compressed version

share|improve this answer
    
Thanks, it looks like a handy app, but it's not exactly the solution I was looking for. I'd really like something generic that can watch files and perform any custom command on changes. –  Gerry Aug 7 '12 at 13:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.