Launchd WatchPath Arguments?

Hi all,

I wish to create a small script that takes a file in a directory, gets its metadata (with 'mdls') and appends this to the end of a text file.

This is not a problem when 'batch' processing the contents of an entire directory. However, I wish to set the dir as a launchd 'watchpath' and have it call the script whenever a file is added. This has created several problems for which I cannot see a solution...

1) How can I identify which files have been added to the directory and pass these as arguments to the script? If that is not possible, can I identify them by some other methd?

2) As the script would be executed on ANY modification of the directory, how can I distinguish between the addition of files and the removal of files. As this is a log of all files that have passed through the dir, I don't wish to overwrite the txt file when something is removed.

Should I forget using launchd and use folder actions? Off the top of my head, I think an applescript would do the trick.

Cheers for any help

G4 450 Sawtooth Mac OS X (10.4) 720Mb ram

G4 450 Sawtooth, Mac OS X (10.4), 640Mb ram

Posted on Feb 3, 2006 8:13 AM

Reply
3 replies

Feb 3, 2006 4:09 PM in response to Rob Cowie

Hi Rob,
I can't help you decide between Folder Actions and WatchPaths except to suggest that it boils down to whether you are more comfortable with shell scripts or AppleScripts. I hope that it doesn't surprise you that on this forum you're going to hear a number of votes for shell scripts and that's certainly the way I lean. I'm far better at shell scripts than AppleScripts and I would say that it's because of the difference in the quality of the documentation.

I use something in the rough direction of what you describe. I call it newlog and it's just a three line script:

date
diff ~luminis/.new_logs.txt <( find $CP_ROOT -name " .log" -ls )
find $CP_ROOT -name " .log" -ls >| ~luminis/.new_logs.txt

The output isn't pretty but it tells me every log file that has changed since the last time the script was run. It does this by searching for the log files and dumping the output into a file. The output is a long listing of the file. The next time it runs it performs the search again and uses the "diff" command to compare the latest search with the previous "dumped" one. Then it runs the search another time, again dumping the output into the file to "freshen" it. The part of the listing that changes will of course be the modification date. That way I can get a report of which log files have changed.

You can do something similar but you probably don't need the "find" command. You can just do a listing of everything and save it. Then each time the script is run, it compares a new listing with the saved one. If you use the "diff" command to do that, its output will contain arrows ('<'re really doing is keeping a text copy of records that are already kept elsewhere in the system. For what do you need this?

Anyway, if you need help with this script, provide some details and I'll help you write it. I've done stuff like this before. In fact, I have a script that compares directories with what the contents should be as predicted by the BOM files in the installer package receipts.
--
Gary
~~~~
If you couldn't find any weirdness,
maybe we'll just have to make some!
-- Calvin

Feb 5, 2006 10:12 AM in response to Gary Kerbaugh

Thanks for the reply. I have more experience with Applescript but I'd like to implement it with a shell script - it's about time I learnt how to do such things!

Specifically what I want is to maintain a text document that contains timestamped entries that indicate when a file has been added to a directory, when a file is modified and when a file is removed. It should be cumulative (i.e. list all changes - not just represent the current state of the dir).

An entry might look thus:

2006/02/02: <filename.ext> <modified|added|removed>

Of course, getting a script to launch on a change to the dir is easy with launchd. What I need to understand is how to ascertain what has happened.. a deletion, modification or addition, and which file was involved.

I'm not at my mac at present but when I am I'll look at your reply more closely and see what I can think of.

Thanks again for your help

G4 450 Sawtooth Mac OS X (10.4) 720Mb ram

Feb 6, 2006 7:34 PM in response to Rob Cowie

Hi Rob,
I'm sorry for the slow reply as I developed the answer before you responded. However, I enjoyed a SuperBowl party out of town. Anyway, I wrote a short script that creates a hidden file in the same directory as the directory whose contents are being watched. The script should work for files and directories that have spaces in the name. In the hidden file the script stores the contents of the directory each time the script is run and stores changes in two variables: one for files that have disappeared and one for files that are new. Here is the script:

#!/bin/bash

DATE=""
TARGET="$1"
TARGET_BASE="$( basename "$TARGET" )"
TARGET_DIR="$( dirname "$TARGET" )"
if [ -e "$TARGET_DIR"/."$TARGET_BASE" ]; then
DATE="$( ls -l "$TARGET_DIR"/."$TARGET_BASE" | awk '{ print $6, $7, $8 }' )"
fi
touch "$TARGET_DIR"/."$TARGET_BASE"
DIFF_LIST=$( diff <( find "$TARGET" | sort -u ) "$TARGET_DIR"/."$TARGET_BASE" )
MISSING=$( echo "$DIFF_LIST" | sed -n 's/^> //p' )
NEW=$( echo "$DIFF_LIST" | sed -n 's/^< //p' )
find "$TARGET" | sort -u >| "$TARGET_DIR"/."$TARGET_BASE"

This doesn't "notice" files that have simply been modified but that's a relatively simple extension. It would require obtaining a long listing of each file with the "-ls" option of the "find" command and saving it in the hidden file. One would also have to search the MISSING list for each file in the NEW list to distinguish the new files from newly modified files.
--
Gary
~~~~
briefcase, n:
A trial where the jury gets together and forms a
lynching party.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Launchd WatchPath Arguments?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.