0

I have a simple AppleScript (application) that runs a shell script:

do shell script "sudo killall -TERM java"

which fails with "sudo no tty present and no askpass program specified".

The desired behavior here is to have the OS X password dialog open (hopefully with an offer to save in the Keychain for future use), but (I'm guessing) I'm missing a step that tells the script to launch the necessary "asker" (which surprisingly, doesn't happen by default).

I've tried

do shell script "ssh-askpass Sudo Password | sudo killall -TERM java"

but get an error with that too.

What is the correct way to invoke the OS X password dialing from AppleScript?

4

1 回答 1

4

The preferred method is indeed:

do shell script "sudo killall -TERM java" with administrator privileges

As you noted, this does not let you memorize the password in your keychain. This is deliberate -- the keychain is intended to save you from having to memorize passwords for services and such, not to defeat the local security policies (which is essentially what you're trying to do.

You might be able to fake it by using the security command to store & retrieve the password (see discussion & sample code at MacScripter). You can prompt for the password with Tell application "System Events" to display dialog "Password:" default answer "" with hidden answer, although I don't know a way to add the "remember in keychain" checkbox. This also raises some security issues, because I don't think there's a way to make a security policy that allows your script to access it without prompting the user for confirmation without also allowing other scripts similar access.

于 2012-07-13T18:49:38.160 回答