QueueDirectories launchd runs again when folder items removed

Hi All,

I’m having a bit of an issue with launchd that’s watching a folder for a new file. My launchd is told to run a script whenever a file is added to a specific folder. This part is working perfectly, my script runs, moves the file to another directory and then deletes any hidden files that may have been created in the watched folder. The trouble is 10 seconds later the script runs again. The only reason I can think that it’s running again is because the original file was moved from the watched folder. In documentation I’ve read it says that you must ensure that the folder is empty so moving the file is a necessity but it doesn’t say that moving the file will cause the launchd to run again.

Here’s my launchd:

<?xml version="1.0" encoding="UTF-8"?> Label com.evanesco.watcher ProgramArguments open /Library/Scripts/Evanesco_Control.app QueueDirectories /Users/nikj/DSLRRemotePro

Here’s the applescript:

set jpgFolder to (path to home folder as string) & "DSLRRemotePro"
set EvanescoFolder to (path to home folder as string) & "Evanesco_Processing"

set contentsList to every paragraph of (do shell script "ls -1 " & quoted form of POSIX path of jpgFolder)
display dialog "Running" giving up after 3

if (count of items of contentsList) > 0 then
	try
		do shell script "mv " & quoted form of POSIX path of jpgFolder & "/*" & space & quoted form of POSIX path of EvanescoFolder
	end try
	
	try
		do shell script "rm -rf " & quoted form of POSIX path of jpgFolder & "/* " & quoted form of POSIX path of jpgFolder & "/.*"
	end try
end if

Any help/thoughts gratefully appreciated,
Nik

Hi,

the launchd agent is triggered whenever the watched path is a directory, the directory is not empty and has been modified. This includes any write operations like adding, removing, renaming and changing files.

Unfortunately It’s up to the scripter to determine what happened and what file is affected.

I recommend to use a compiled script rather than an application because the agent could be called frequently.

Hi Stefan,

Many thanks for your reply.

So would you recommend I change my launchd agent to something like this:

osascript /Library/Scripts/Evanesco_Control.scpt

Thanks,
Nik

Yes, maybe you have to put osascript into both Program and ProgramArguments keys like

. <key>Program</key> <string>/usr/bin/osascript</string> <key>ProgramArguments</key> <array> <string>osascript</string> <string>/Library/Scripts/Evanesco_Control.scpt</string> .
providing the full path in the Program key

I also recommend to run the script from the user scripts folder as the agent is located in the user library anyway

Hi Stefan,

Incase anybody has the same questions, I took your advise and resaved my app as a compiled script and saved into my users scripts folder and amended the launchd as below:

<?xml version="1.0" encoding="UTF-8"?> Label com.evanesco.watcher ProgramArguments osascript /Users/nikj/Library/Scripts/Evanesco_Control.scpt QueueDirectories /Users/nikj/DSLRRemotePro

Many thanks,
Nik