Would this be a good approach for a watch folder?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
japino
Posts: 78
Joined: Sun Oct 14, 2012 10:56 am

Would this be a good approach for a watch folder?

Post by japino » Sun Nov 11, 2012 6:55 am

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!

dave_probertGA6e24
Livecode Opensource Backer
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?

Post by dave_probertGA6e24 » Sun Nov 11, 2012 8:28 am

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
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

japino
Posts: 78
Joined: Sun Oct 14, 2012 10:56 am

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

Post by japino » Sun Nov 11, 2012 9:32 am

Hi Dave,
This looks great, thanks very much!
japino

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

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

Post by shaosean » Sun Nov 11, 2012 2:35 pm

Just make sure that the file is 100% there before you start processing it..

japino
Posts: 78
Joined: Sun Oct 14, 2012 10:56 am

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

Post by japino » Sun Nov 11, 2012 5:18 pm

Shouldn't be a problem because the files I'm talking about should all be less than 1 KB :)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

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

Post by Mark » Mon Nov 12, 2012 2:28 am

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
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

Post Reply