Mac – How to run a script or app as sudo with a click

macsudo

I have a certain program that doesn't work right unless I run it as sudo. I don't mind having to type in my password, but I'd like to get out of the whole process of opening command line and typing "sudo ./"+script name. If you create a script that runs as sudo and click it, it just fails due to permissions. Is there a way to do this more quickly?

Best Answer

Run sudo visudo (or EDITOR=nano sudo visudo) and add a line like this:

%admin ALL=NOPASSWD: /usr/bin/myscript

Then create a .command file that runs /usr/bin/myscript:

echo sudo myscript>~/Desktop/test.command
chmod +x ~/Desktop/test.command
echo $'#!/bin/sh\nls /var/root'|sudo tee /usr/bin/myscript;sudo chmod +x /usr/bin/myscript

Or if for example you want to run sudo purge without having to enter a password, add

 %admin ALL=NOPASSWD: /usr/sbin/purge

to sudoers.