11

Anyone know about a program for Mac OS that you can pass the name to an executable and a file to watch, so that it then runs the executable everytime the file being watched changed?

I have something like this in mind:

$ fwatch /Users/foo/doc.tex /Users/foo/run-pdflatex.sh &
  fwatch running. Listening for changes in /Users/foo/doc.tex.
$ echo "This aint no LaTeX" > doc.tex
$ fwatch: Change in /Users/foo/doc.tex detected. Running /Users/foo/run-pdflatex.sh...

3 Answers 3

8

You can set this up using launchd. Specifically, you need to use the watchpaths key in your launchd plist to look for changes in that file.

Exhaustive information available in this AFP548 article.

Although it appears it's no longer under development, Lingon is an application providing a GUI way to set up these launchd plist files.

EDIT:

Roughly, here's what you'd do using your sample paths above. Save a file named com.superuser.run-pdflatex.plist with the following contents to ~/Library/LaunchAgents

<?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.superuser.run-pdflatex</string>
    <key>OnDemand</key>
    <true/>
    <key>Program</key>
    <string>/Users/foo/run-pdflatex.sh</string>
    <key>ProgramArguments</key>
    <array>
            <string>run-pdflatex.sh</string>
    </array>
    <key>WatchPaths</key>
    <array>
            <string>/Users/foo/doc.tex</string>
    </array>
    </dict>
</plist>

Then you'd type launchctl load ~/Library/LaunchAgents/com.superuser.run-pdflatex.plist in the terminal to load your file-watching daemon.

Above is untested, but that's the general idea.

3

I've found fswatch which claims to be "a cross-platform file change monitor", and seems to support all the proper platform solutions like Filesystem Events on macOS.

# Monitor all files in a directory, execute script once when something changes
fswatch -o -r path/to/directory | xargs -n1 -I{} your-command-here
2

You can do it in Automator. It is under Folder Option in the automator workflow I believe (not in front of a Mac). Then you just attach the script to the folder and it will fire every time a file is added/changed.

2
  • 1
    I can't quite follow. I'm new to Automator, but as far as I know you can't choose when to execute your script but only what to do when it is run. There's the folder actions in Mac OS that I could use also but I figured AppleScript only has the action "on adding folder items to" but nothing like "on changing folder items in".
    – Johannes
    Jul 15, 2010 at 16:22
  • You are correct @Johannes. While there are folder actions for adding folder items, removing folder items, moving the folder window, closing the folder window, and opening the folder, there is no Folder Action for detecting changes to files. You can see this by opening the StandardAdditions.osax dictionary in Applescript Editor.
    – ghoppe
    Jul 15, 2010 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .