How to Authenticate AppleScripts with Administrator Privileges
Posted 09/21/2011 at 1:35pm
| by Cory Bohon
We’ve covered writing basic AppleScripts in the past, but if you’re writing an AppleScript that requires administrator privileges to run properly, the script may not function correctly without a username and password present. In this post, we’ll show you how to correctly format your AppleScripts that require authentication.
Whenever you use an AppleScript to perform an administrator-privileged task on your Mac, you must authenticate as an administrator. This includes Unix-based applications and scripts, or changes you may do to you through the command line.
To ensure your script runs properly, open the AppleScript Editor (located in /Applications/Utilities), and type in the following code:
do shell script "ScriptHere" with administrator privileges
Replace “ScriptHere” inside of the quotes with the Unix command that you wish to run.

After clicking the Run button in the AppleScript Editor, a dialog will prompt you to type in the administrator username and password for the system the script is running on.

This code will let you run a shell script with administrator privileges turned on. This method is better than hard coding your username and password into the script, because it provides better security and the ability to re-use the AppleScript on a different system or with different users.
Follow this article’s author, Cory Bohon on Twitter.