MacOS – How to open a shell script in a new Terminal window and run it with administrator privileges

administratorapplescriptcommand linemacossudo

I need to programmatically open a shell script in a Terminal window and run it with administrator privileges. I'm using osascript as it displays a convenient prompt for an admin login/password.

I'm currently using this :

osascript -e 'do shell script "open -a Terminal \"'"$appDir"'\"" with administrator privileges'

The trouble is, even after entering the credentials in the OS X prompt, the newly opened script will beg for a password at the first sudo command.

How do I pass the admin credentials to the opened script?

Best Answer

I'd try writing a simple script:

#!/bin/bash
sudo /usr/bin/id

save this as something.command, change its permissions to executable with chmod +x, and then run this from AppleScript with

osascript -e 'do shell script "open -a Terminal ./something.command"'

Instead of /usr/bin/id you can call whatever script you need to run with admin privileges then.

EDIT:

This will work:

osascript -e 'do shell script "sudo /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal" with administrator privileges'