1

I have a shell script that is run by an AppleScript app. That shell script restarts the machine at some point with shutdown -r now.

How can I avoid having the AppleScript application automatically launch itself again after the reboot?

  • The app is not included in the startup items
  • The app is launched again just like any other open app when the shell script restarts OS X

The sequence is this:

  1. I double click on the AppleScript app
  2. AppleScript launches a shell script in Terminal
  3. The shell script, at some point, reboots the system
  4. The AppleScript app starts again after the reboot
  • Need more info... how is the app launching itself now? Did you add the app to your startup items? – Flying Trashcan Dec 9 '14 at 15:52
1

Have your AppleScript write an empty file to a predefined location and test for its existance when the script starts. If the file is already there, delete it and quit the script; otherwise proceed as normal and reboot.

1

You can try to redirect the shell command with

do shell script "insert_your_command &>/dev/null &"

and then tell you app to quit

EDIT
You can add a delay before redirecting the command as well.

 do shell script "(sleep 5 ; insert_your_command) &>/dev/null &"
  • The app may not have time to quit. Maybe have the shell script quit it with osascript -e 'tell application "Script Editor" to quit' before it does the shutdown. But you still need to detach the shell script with the trailing &, or it'll get killed when the app quits. – ganbustein Dec 9 '14 at 22:55
  • The OP's AppleScript app needs to be told to quit, not "Script Editor". – adayzdone Dec 9 '14 at 23:54
0

My guess is OS X is restoring the Apple Script App state after it restarts. You could possibly try and have the Apple Script delete it's own application state right before the restart command, though I haven't verified that this works, or is possible. It should be located in, ~/Library/Saved Application State/.

  • I don't think AppleScript can know when to delete its state before the restart, as the restart is initiated through a shell command. – DavidD Dec 9 '14 at 16:27
  • But as you said, the shell script is run by the Applescript app. Is the app still open when the shell script is run? If so, it could be restoring its state upon reboot. – Flying Trashcan Dec 9 '14 at 16:32
  • Yes the app is still open when the shell script is launched (in a Terminal window) – DavidD Dec 10 '14 at 17:47
0

Ok found it.

The shell script needed to have the following lines:

osascript -e 'tell app "System Events" to restart'
killall "Terminal"

The shell commands above initiate the restart through an ad hoc Apple Script command, but kill the Terminal immediately after. This has the effect to quit the Apple Script app just before the OS X restart process does it. Therefore, OS X doesn't put the Apple Script app in the list of open apps that need to be re-opened after the restart.

Two side effects of using this method:

  • rebooting through that Apple Script command leaves a chance to the user to save unsaved work from open apps, unlike the shutdown -r now shell command which forces an immediate reboot.
  • Terminal still gets abruptly killed, which may be an issue if processes are running in other Terminal windows/tabs

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .