Page 1 of 1
Watch Folder and Loop problem
Posted: Fri Apr 27, 2012 12:40 am
by ScottM
Hi All,
I have a problem that I am trying to solve. I want to create a watch folder that is constantly checked for new files after it has finish copying (or growing) and perform a task when a new file has been dumped into that folder. The user starts and stops the watch folder process via a button. My attempts have not been successful as when i start the loop the button is ignored because I have a loop in progress. I dont have code for this as I keep starting from scratch trying to solve this basic problem before I create my program as the watch folder is a major part of it. I know it is not much to go on but thanks for any help in advance.
Kind Regards,
Scott
Re: Watch Folder and Loop problem
Posted: Fri Apr 27, 2012 1:18 am
by sturgis
The problem is most likely that your loop isn't allowing time for other things to happen. There are a few ways around this i'll post a couple next.
If you want to use a repeat loop you need a way to make it stop of course so, say you have a button to start and stop the loop and have all your code in the button.
Code: Select all
local sRunning
on mouseUp
if sRunning is empty then put true into sRunning --init the script local variable if its empty
put not sRunning into sRunning --this will toggle the value of sRunning to its opposite boolean on each click
watchLoop --the loop handler
end mouseUp
command watchLoop
repeat while sRunning --only repeat if sRunning is true
wait 10 milliseconds with messages -- wait with messages allows room for other things like button clicks to occur
//the rest of your code goes here. Clicking the button that toggles srunning will stop the loop
end repeat
end watchLoop
Or you can use a send in time.
Code: Select all
local srunning
on mouseUp
if sRunning is empty then put false into sRunning
watchLoop
end mouseUp
command watchLoop
if sRunning then --if we should be looping (sRunning is true)
--do stuff here
send "watchLoop" to me in 1 tick -- if sRunning is true, watchloop calls itself in 1 tick
end if
end watchLoop
Re: Watch Folder and Loop problem
Posted: Fri Apr 27, 2012 1:55 am
by ScottM
Thanks Sturgis I will give this a go. I have not used COMMAND to create a handle only ON, was wondering what is the difference? Cheers Scott.
Re: Watch Folder and Loop problem
Posted: Fri Apr 27, 2012 2:09 am
by sturgis
I just tend to use command rather than on so that I can just glance at a script and see which handlers are mine and which are lc messages. Don't actually recall if there is a real difference or not. If you prefer on that works fine too.
Re: Watch Folder and Loop problem
Posted: Fri Apr 27, 2012 2:23 am
by ScottM
Thank you for your time. Your idea works well. My script tests I created my loop was outside of the button script, this is where I was going worng. I looked up command in the dictionary (was lazy in asking

) and it performs the same function. Cheers, Scott.
Re: Watch Folder and Loop problem
Posted: Fri Apr 27, 2012 2:42 am
by sturgis
You can do it outside the button script no problem, you just have to make sure you have a way to break the loop. Could place the loop part in the card or stack and set a property to track whether the loop should run or not. Or have a handler in the same location as the loop script that is identical to the one in the mouseup, then the mouseup just calls that handler, and that handler starts the loop.
Lots of ways to accomplish your goal, where a script most logically goes can change depending on how and what you are trying to accomplish.
Glad you got things working!