Page 1 of 1

Would this be a good approach for a watch folder?

Posted: Sun Nov 11, 2012 6:55 am
by japino
Hi there!

I want to use a watch folder on OS X. A watch folder for me is a folder that should be constantly monitored for new files that are added to it. Every time a new file is added to this empty folder, a script should run. Fles that have been processed by the script should afterwards be moved automatically to another folder.

I'm thinking of using an endless repeat loop which constantly checks if the folder is empty or not. Would that be a good approach? Or is it overkill to have an application that is constantly running at full speed in situations where new files may be added only a few times a day? But I do want the script to run shortly after a file has been added, within a minute or so.

May be using an Automator folder action which then runs a Livecode application would also be a solution?

Just asking the experts here what they think would be the best approach before I start writing code.

Thanks!

Re: Would this be a good approach for a watch folder?

Posted: Sun Nov 11, 2012 8:28 am
by dave_probertGA6e24
Instead of it running at full speed - ie every second or faster! Simply set it to call a handler every minute or so - that should be adequate for most watch folder actions. If the tool detects that a new file has been added then it continues to process it, and it could increase the frequency of checking for a limited time e.g.. every 10 seconds for the next minute.

Use the "send" command to do this and not a repeat loop. If you use a loop then your program will be blocking and using more CPU than a timed send call. If you haven't used a send call then take a look in the dictionary - it's a very useful command.

The below code is a type of recursive call routine that can be modified for auto-repeating processing.

Code: Select all


on callback_checker
  if the_folder_has_a_file() then  // some folder checking routine
    //do whatever is needed here
    send "callback_checker" to me in 10 seconds
  else  //no file yet
    send "callback_checker" to me in 1 minute
  end if
end callback_checker
Something like that should do the job in most cases.

Cheers,
Dave

Re: Would this be a good approach for a watch folder?

Posted: Sun Nov 11, 2012 9:32 am
by japino
Hi Dave,
This looks great, thanks very much!
japino

Re: Would this be a good approach for a watch folder?

Posted: Sun Nov 11, 2012 2:35 pm
by shaosean
Just make sure that the file is 100% there before you start processing it..

Re: Would this be a good approach for a watch folder?

Posted: Sun Nov 11, 2012 5:18 pm
by japino
Shouldn't be a problem because the files I'm talking about should all be less than 1 KB :)

Re: Would this be a good approach for a watch folder?

Posted: Mon Nov 12, 2012 2:28 am
by Mark
Hi,

A folder action would definitely be a good idea. The folder action only needs to open the app, which would then check if there are any files and process them. You don't need automator for this. Use the Folder Actions Setup app in the folder /System/Library/CoreServices.

Kind regards,

Mark