I've discovered another method to restart an AppleScript.
This method is not "better" than the other methods that have been provided; it is merely just another way of approaching things. Here's how you can restart an .scpt file in AppleScript:
if buttonChoice is "Start over" then
run script "/Users/Me/Desktop/My script.scpt"
error number -128 (* user cancelled *)
end if
Or, alternatively:
if buttonChoice is "Start over" then
run script (path to me)
error number -128 (* user cancelled *)
end if
This code simply creates another instance of the currently running AppleScript .scpt file, and then quits the current instance.
I learned about run script
from a comment by user Camelot on the following webpage:
Call Another Applescript | Apple Communities
With the same underlying premise, if you want to restart an .app file using AppleScript, then use this:
if buttonChoice is "Start over" then
do shell script "open -n " & quoted form of "/Users/Me/Desktop/My app.app"
error number -128 (* user cancelled *)
end if
Or, alternatively:
if buttonChoice is "Start over" then
do shell script "open -n " & (quoted form of (POSIX path of (path to me)))
error number -128 (* user cancelled *)
end if
The -n
allows a new instance of the application to open, even if an instance of this application is already running. The -n
is necessary because, practically speaking, the AppleScript app technically closes after the second instance has been created, even though this may not appear to be the case based on what one sees on their screen.
When should one use this method of restarting?
This is going to get a little confusing, so bear with me...
I have an AppleScript .scpt file that automatically runs when my computer wakes from sleep. (I use SleepWatcher to accomplish this.)
This .scpt file calls a specific subroutine of a different .scpt file; it skips over the entire script, and only runs the code that is found in one, specific subroutine. (To accomplish this, I use the method described here.)
This second .scpt file is rife with "Start over" buttons in nearly every dialog, employing:
return on run
to achieve the restarting effect. (This restart method was provided in a previous answer to this question.)
Now, the problem is, while my first .scpt file does in fact successfully restart the second .scpt file when return on run
is used in the second .scpt file, once the script has completed, a dialog box containing the following error is presented to the user:
«script» doesn’t understand the “return”
To be fair, this error makes sense; the first .scpt file cannot fully comprehend the significance of return
, because it was never truly running the other script. It was only running one subroutine of the other script.
This error bugged me. So, I messed around with the code some more, and learned about run script
. The specific type of scenario that I've just explained is where the run script
restart method truly shines.
The simple run script
method saves the day: it restarts the second .scpt file, and without having to invoke the return
command in the process. Thus, this method is completely error-free, even when being run outside of its larger, surrounding code. This is the only method on this page that can do this, as the other methods are reliant on return
.