Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have written an Automator application with the following shell script to open an FTP connection on my system:

sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
osascript -e 'tell app "Finder" to display alert "SFTP Opened"'

This works exactly as expected except that the sudo command needs a password. When the same command is run on Terminal, it prompts me for a password but when run as an Automator app, doesn't prompt me for any input. Is there anyway to programmatically pass the password to this sudo within the Automator script? I know this is not a safe practice but I still want to do it as my system is reasonably secure and untouched by anyone other than me.

A "do shell script...with administrator privileges" method is giving the following error:

enter image description here

share|improve this question

2 Answers 2

up vote 1 down vote accepted

AppleScript should let you do this by using with administrator privileges:

osascript -e 'do shell script "sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist" with administrator privileges'

This doesn't pass the password, but prompts for it with a standard OS X prompt:

You can use this in your Automator workflow in one of the two following ways:

share|improve this answer
    
This is throwing a syntax error: "A unknown token can’t go after this identifier." –  Amit Schandillia May 10 at 18:34
    
It's still giving the same error :( –  Amit Schandillia May 10 at 18:36
    
Yes it's a single line...please see the screenshot in the question update. –  Amit Schandillia May 10 at 18:43
    
@Amit osascript is for bash :) If you want pure AppleScript in the AppleScript Automator action, remove the osascript like this: do shell script "sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist" with administrator privileges (Do one of the following from this picture) –  grgarside May 10 at 18:44
1  
Yes, this worked like charm! Thanks for your patience...silly me! –  Amit Schandillia May 10 at 18:47

expect

You could instead use a tool like expect to script the interaction with sudo. expect is designed for this situation.

expect - programmed dialogue with interactive programs

The following shell script may be enough for your two commands:

#!/usr/bin/expect -f
spawn sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
send "my-secret-password\n"

AppleScript and with administrator privileges

An alternative approach is to use AppleScript's do shell script command. The command offers a with administrator privileges option. Apple's Technical Note 2065 covers do shell script in detail.

echo

Other common approaches to this problem involve using echo to pass the password to sudo with a pipe. This StackOverflow answer discusses this and other related approaches, How to provide password to a command that prompts for one in bash?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.