Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

sudo root Password Prompt

Hi, I've built a widget that allows the user to modify the power settings wake-up schedule.

I did this through the pmset Unix command, like this:

widget.system("/usr/bin/sudo pmset repeat wake MTWRFSU " hours+":"+minutes":00", null);

I am forced to use sudo, because OS X's security settings block the widget's access to pmset. The issue I have is that since the sudo command returns a password prompt in the Terminal (in the Unix console), on the user side nothing happens, when they use the widget. The whole process is cancelled due to an invisible password request.

The question I am asking is, is there a way to prompt the user for the root password through OS X's default user password dialog box, instead of prompting through the background running Unix console? Is there a Unix or AppleScript command that does this?

And if there isn't, is there a way to send password information (that was entered in the widget) directly to the Unix console?

iMac (Intel Core 2 Duo), Mac OS X (10.5.7)

Posted on May 17, 2009 5:57 AM

Reply
4 replies

May 17, 2009 8:15 AM in response to kenoi

Applescript allows you to run a shell command using "Administrator privileges" (basically like sudo, only better).

The following AppleScript will run "/bin/ls -la" with root privileges by prompting the user in a standard Authorization Services dialog:


do shell script "/bin/ls -la" with administrator privileges


(Note: you do not need to use sudo in there anywhere, that's handled for you automatically; simply list the process you want to run and the options).

If you knew the user's password prior to running this command, you could do the following:


do shell script "/bin/ls -la" with administrator privileges password "myPlainTextPassword"


Note that this isn't as secure as letting AuthorizationServices handle the password prompt.

You may want to play around in Script Editor (/Applications/AppleScript/) until you get the command syntax down right. Then, you can use the "osascript -e" shell command to execute the script from your widget.

For example, the following shell command would have basically the same effect as executing the above-mentioned AppleScript:


osascript -e "do shell script "/bin/ls -la" with administrator privileges"



So, to review, in case your head is starting to swim a bit... You're executing a shell command (osascript) to execute an AppleScript which in turn uses AuthorizationServices (prompt for password) to perform a shell command with root privileges.

Hope this helps...

May 17, 2009 6:14 PM in response to MarkDouma®

Excellent, this is exactly what I was after! You've answered both of my queries; thank you, Mark.

I've managed to get the quotations right, and implement it like so:

widget.system("/usr/bin/osascript -e 'do shell script \"/usr/bin/pmset repeat wake MTWRFSU " hours+":"+minutes":00\" with administrator privileges'", null);

I also realize, that all the AppleScript in this piece of code, can be saved as a .scpt file within Script Editor, and run when needed with the osascript command, like so:

widget.system("/usr/bin/osascript filename.scpt", null);

However, I still have one question.

The password prompt now, when activated, says "osascript requires that you type your password". Is there a way to change "osascript" to the widget's name? Can I name the script somehow, to make it clear for the user, where the request is coming from?

sudo root Password Prompt

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.