Sudo is “a program for some Unix and Unix-like computer operating systems that allows users to run programs with the security privileges of another user (normally the superuser, a.k.a. root).” (Wikipedia reference). It works great for command line applications – especially for running them from a terminal prompt, or as part of a shell utility.
For Mac OS X Cocoa-based apps, there is analagous ability to sudo provided via the Authorization Services API. Use of the API allows you to prompt the user for their username and password requesting the ability to escalate privileges.

System Preferences app's use of Authorization Services API
The Authorization Services API is a bit complicated. All the more so if you’re not writing a Cocoa app in Objective C. For example, maybe you’re using a scripting language, or you’re writing your GUI in Java / Swing for cross-platform use.
For that case, I’ve written a small utility that I’ve dubbed cocoasudo. Use cocoasudo in much the same way you’d use sudo. However, instead of users being prompted for their password in a Terminal window, they’ll get a dialog prompt via the Authorization Services API.
Usage:
cocoasudo [--icon=/path/to/icon.icns] ["--prompt=This prompt will be displayed to users."] command-to-launch [command-arguments...]
Both the –icon and –prompt parameters are optional and may be omitted.
Download the source on Github, or via your git client:
git clone git://github.com/performantdesign/cocoasudo.git
Or, if you don’t have Xcode, or don’t want to compile from source, you can download the cocoasudo binary directly here.
cocoasudo is Apache-licensed, so you can mostly use it as you see fit.
October 27th, 2009 at 1:10 pm
Dude, I’ve been looking for a program just like this for the past week. I searched today and bam, here it is. Just like gksudo on Linux. Just one problem. I am having issues getting the command and arguments to run. Not sure if this is because it is not recieving the correct authentication or if it is just a syntax error on my part.
ex.
./cocoasudo installer -pkg /Users/distrotubux/Desktop/Setup.mpkg -target / -verbose
I get the prompt for pw but the installer does not run. I’ve tested this command using sudo and it works just fine. Ideas?
October 27th, 2009 at 1:39 pm
Glad this can hopefully meet a need.
Could you give these alternates a try? It could be that the path of what you are running is required. If this works, I’ll work on updating the utility to search the path as well.
Alternate #1:
./cocoasudo `which installer` -pkg /Users/distrotubux/Desktop/Setup.mpkg -target / -verbose
Alternate #2:
./cocoasudo /usr/sbin/installer -pkg /Users/distrotubux/Desktop/Setup.mpkg -target / -verbose
October 29th, 2009 at 9:27 am
Yup, that did the trick. Both commands work. Thanks again for this nice little app.
October 29th, 2009 at 10:21 am
Another problem. My script is unable to catch the output of a command that is run with cocoasudo. Seems that cocoasudo is catching all the stdout from the run command and it is never getting to stdout.
October 29th, 2009 at 11:07 pm
A few updates were just pushed to git:
- stdout works now
- path is now searched for executable
- OS X 10.4 support
- improved error handling & messages
October 30th, 2009 at 4:02 pm
That’s great. Works like a charm!