Last Updated: February 25, 2016
·
1.081K
· ruidovisual

Prevent applescript stopping while running shell script with administrator privileges

If you need to do a shell script via applescript as root (administrator privileges), without user interaction (no prompt for password), and without killing applescript (it waits until the script responds, but there are scripts with no response at all like [...]/xampp start), try this:

do shell script "echo <your password> | sudo -S <command to execute> &"

The parts:

  1. Appending an ampersand to the command, will tell it to continue without waiting for output.
  2. Normally, you should use the with administrator privileges statement at the end of the do shell script line, but if you don't want the user to be prompted for it's password, you have to add password <your password> at the end of the line... Downer: The ampersand at the end of the command you want to execute, will no work if you do this

The Downsides

  1. The password will have to be in plain text, but hopefully you will not use it in a very sensitive environment. (Encapsulating the script as application might add a "layer of security".)

With all that considered, enjoy!