Can an applescript start over while running?

I have an applescript that is run from filemaker pro that interacts with web pages.
sometimes and error happens and I need to start the script over. Can an applescript run itself again with a conditional statment?

My old days of programming would have it go to a line of code (line 1). Is there anything comparable in applescript.

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…

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…

on doit(theFlag)
	if theFlag then
		display dialog "I ran"
		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…


You can also use script objects within your script. Script objects are like scripts within a script. There are different ways of using the script object with different effects. Here’s an example using ‘run script’ to run the script object:

global _err
---------- scripts
script ErrCheck
property err_flag : false
property i : 0

set i to i + 1
display dialog (i as string) buttons ¬
{“Quit”, “Cancel”, “Repeat”} default button “Repeat”
set br to button returned of result
if br is “Quit” then exit repeat
on error
set err_flag to true
exit repeat
end try
end repeat
return err_flag
end script

set _err to (run script ErrCheck)
if not _err then exit repeat
end repeat

I was trying to think of a better example perhaps using an idle handler. If I come up with something I’ll write back.



Here’s an idle handler with reopen handler:

global i

on run
set i to 0
end run

on reopen
end reopen

on idle
set i to i + 1
display dialog (i as string) buttons ¬
{“Quit”, “Cancel”, “Continue”} default button “Continue”
set br to button returned of result
if br is “Quit” then quit
on error
end try
return 1
end idle

This scirpt should be saved as stay open application.