Would this be a good approach for a watch folder?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Would this be a good approach for a watch folder?
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!
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!
-
- Livecode Opensource Backer
- Posts: 328
- Joined: Mon Dec 05, 2011 5:34 pm
- Contact:
Re: Would this be a good approach for a watch folder?
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.
Something like that should do the job in most cases.
Cheers,
Dave
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
Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.
Visit http://electronic-apps.info for released App information.
Re: Would this be a good approach for a watch folder?
Hi Dave,
This looks great, thanks very much!
japino
This looks great, thanks very much!
japino
Re: Would this be a good approach for a watch folder?
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?
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?
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
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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode