Well, ok, here's the beginning of a solution. First, use the launchd plist file that you already have, with the following modification (I'm going to assume the name of this plist file is "Watch Paths Job.plist"):
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/PATH-TO-SCRIPT/Watch Paths Script.scpt</string>
</array>
Second, create an empty plist file with the name "Storage File.plist". Either use the plist editor or make a copy of Watch Paths Job.plist and delete everything between <dict> and </dict>
Now open the applescript editor and copy the following script into it, saving it as an applescript with the name "Watch Paths Script.scpt".
tell application "System Events"
-- jobFile should point to the launchd plist file that calls this script
set jobFile to property list file "/PATH-TO-FILE/Watch Paths Job.plist"
-- storageFile should point to an (initially empty) plist file
set storageFile to property list file "/PATH-TO-FILE/Storage File.plist"
set watchFileList to value of property list item "WatchPaths" of jobFile
repeat with thisFile in watchFileList
set currentModDate to false
try
-- compare current modification date to stored modification date
set currentModDate to modification date of filethisFile
tell storageFile
set oldModDate to value of property list itemthisFile
end tell
if currentModDate > oldModDate then
-- if mod date changed, run script and update stored date
my actOnChangedFile(thisFile)
set value of property list itemthisFile to currentModDate
end if
on error errStr number errNum
if errNum = -1700 then
-- error reading property list item, most likely trying to read the wrong data type
set value of property list itemthisFile to currentModDate
else if errNum = -1728 then
-- either the property list item or the file to be watched does not exist
if currentModDate ≠ false then
-- file exists, so make new property list item
tell storageFile
makenewproperty list itemat end of property list itemswith properties {name:thisFile, value:currentModDate, kind:date}
end tell
end if
else
-- unknown error, report error so that it can be dealt with
display dialog (errNum & return & errStr) as string
end if
end try
end repeat
end tell
on actOnChangedFile(aFile)
-- I'm assuming here you pass the file path to the shell script as an argument
do shell script "/PATH-TO-SCRIPT/amr-upload.sh " & quoted form of aFile
end actOnChangedFile
It should work as follows:
- launchd sees a change in one of the files in the WatchPaths list and launches the applescript
- the applescript reads the list of watched files out of the launchd plist file, and checks the file modification date on each against the stored data in the storage file
- where necessary, the script launches the amr-upload shell script and updates the modification date stored in the storage file.
The try block does error handling, and currently manages cases where the watched file doesn't exist, or where the plist entry in the storage file is missing or of the wrong data type. If you run across other errors you'll need to note the error number and add a condition to handle the problem.