I am writing an Applescript that aims to automate the install (add to root keychain) and 'Always Trust' setting by simply launching the Applescript. I'm stuck with how to get the admin user authenticated with the authentication popup?

Here is my code so far:

    set question to display dialog "To Install Click 'Continue'" & return & "You will be prompted to enter an administartor password!" buttons {"Close", "Continue"} default button 2
    set answer to button returned of question
    if answer is equal to "Continue" then
        do shell script ("sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/Desktop/MyWiFiCertificate.crt")
    else if answer is equal to "Close" then
        display dialog "You have cancelled the WiFi Certificate installation." buttons {"Exit"} default button 1
    end if
on error errorMsg
    display dialog errorMsg
end try

However, I get this error

sudo: no tty present and no askpass program specified".

I'm guessing it is because of the fact that the operation can not be authenticated by the admin user. I'd be grateful if anyone who has automated this process before could refine my code to enable the user to authenticate and automatically 'Always Trust' the WiFi Certificate.


You can pass administrator privileges to a line of AppleScript which requires administrator privileges by appending "with administrator privileges":

do shell script "security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/Desktop/MyWiFiCertificate.crt" with administrator privileges
  • Is sudo still required then? – nohillside Jan 23 '16 at 15:16
  • @patrix Yes, not ever investigated why though. Guess that's something I could look into and add into this answer. – grg Jan 23 '16 at 15:17
  • @grgarside That seems to have solved it for me, I now get the authentication prompt and looking at the Keychain it is set to 'Always Trust' just as I wanted. But I can't see where in the command it defines 'Always Trust'? – ProGrammer Jan 23 '16 at 22:10
  • @grgarside Sudo is definitely NOT required. Apple specifically recommends against using it in conjunction with "with administrator privileges". Quoted from developer.apple.com/library/mac/technotes/tn2065/_index.html : Note: Using sudo(8) with with administrator privileges is generally unnecessary and creates security holes; simply remove the "sudo". – William T Froggard Jan 24 '16 at 3:23
  • @Pro It's "Always Trust" unless you specifically specify otherwise. – grg Jan 24 '16 at 13:00

You must log in to answer this question.

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