Hybrid Hacker

👾 Technical notes and hybrid geekeries

Ask for sudo password in graphical mode on MacOS X

  • Sun 15 November 2015
  • Code

I do lot of bash scripting under MacOS and usually, if I need administration privileges, I run sudo from my terminal. Anyway, sometimes you need to let non-technical users run these scripts and I'm sure that they wouldn't be happy to open terminal and run your script with sudo command. So how to solve this? Pretty easy, look at the code below.

osascript -e "do shell script \"touch /myfile\" with administrator privileges"

Osascript is a command under MacOS that let you launch scripts written in AppleScripts or any Open Scripting Architecture (OSA) language. As you can see above, this script tries to create (touch) an empty file called myfile under the filesystem root that is not owned by you. To do this, we need root password and using osascript we can ask this using MacOS UI.