4

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?

  • Can't you run the script requiring admin rights directly (instead of using Terminal as an in-between)? – nohillside Nov 15 '14 at 13:20
  • No, because running the script directly executes it in the background. I need a Terminal window for user interaction. – DavidD Nov 15 '14 at 13:28
2

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'
  • Thanks, but this is similar to the current state: something.command prompts for a password, which is what I'm trying to avoid. I would like to have an OS X prompt window, which allows entering another user with admin privileges for example. Osascript offers such a functionality with 'with administrator privileges'. – DavidD Nov 15 '14 at 13:45
  • 1
    Last edit contains the solution – Brethil Nov 15 '14 at 16:57
  • Thank you Brethil, your edit did the trick. I ended up using this line: osascript -e 'do shell script "sudo /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal \"'"$appDir"'\"" with administrator privileges'. Admin privileges asked by the OS X dialog were successfully passed to the executed script. – DavidD Nov 16 '14 at 16:32

You must log in to answer this question.

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