Thare are a couple of ways to do it.
But first…
If you remember how to use “goto 1” in your “old days of programming”, you’ll remember that you can create infinite, or “endless” loops. in other words, you need to be sure you’ve done something to correct the situation that caused the error before you recall your script.
Code which calls itself is called “recursive”. This is common for things like traversing directory trees and similar structures when you don’t know in advance how many branches will be on a tree.
One way is to have a script re-run itself, as you suggested. (That’s called “External Recursion”.) You can do that, just make sure that the script isn’t going to re-run itself more than a few time because you’re sure you’ve corrected the error. Otherwise you’ll be running forever.
Here’s a fun experiment… save this script on your hard drive. Put its path in where I have “Macintosh HD:run.scpt”
run script file "Macintosh HD:run.scpt"
display dialog "I ran again"
When you run it, you’ll get messages over and over and over, until you click the cancel button. That’s recursion.
That’s the answer to the question “Can an applescript start over while running?”, but it doesn’t solve your problem.
Since an applescript’s implicit “On Run” handler doesn’t take parameters, there’s no way to pass in a “conditional statement”, which you said you’re going to need to know so you can do something differently when you re-run the code.
You’ll need to implement an “internal recursion” where the repeated code is in a function that calls itself.
Here’s an example…
doit()
on doit()
display dialog "I ran again"
doit() -- recursion
end doit
See the difference? The script never re-runs, its just got a function that calls itself. The result is the same – repeated messages until you cancel.
Unlike the demo above which repeats endlessly, you’ll be changing the flag when you re-run the function, like this…
doit(true)
on doit(theFlag)
if theFlag then
display dialog "I ran"
doit(false)
else
display dialog "I won't run again"
end if
end doit
The function is called once with a true flag. You see “I ran”. Then the function re-calls itself with a false flag and you see “I won’t run again”. Since that branch of the flag evalution doesn’t re-call the function, that’s the last message you’ll see – even if you click the message’s OK button.
If you want more info about scripts calling scripts, Camelot had some good tips in his recent post in…
http://bbs.applescript.net/viewtopic.php?t=9329&start=0&postdays=0&postorder=asc&highlight=