Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X Python GUI Administrator Prompt

I have an OS X Python application that uses wxPython for it's GUI controls. I'm looking to prompt the user for administrative rights (akin to using the Authorization Service API in Objective-C) before starting a network service.

The closest library I have found is Bob Ippolito's Authorization library but it is fairly outdated and has compatibility issues with Snow Leopard (OS X 10.6.4 / Python 2.6.4).

My workaround would be to create an Objective-C launcher that runs the python application with administrative rights but that feels fairly kludgy.

Thanks for the help!

like image 289
Erik Karulf Avatar asked Feb 27 '23 17:02

Erik Karulf


2 Answers

If it's just a shell command that you need to run, requesting elevated privileges, then you can do this:

os.system("""osascript -e 'do shell script "<commands go here>" " with administrator privileges'""")

It will run a shell command within the applescript interpreter and prompt the user for their account credentials; effectively running the command using sudo.

Note that you'll have fun escaping quotes within your command, e.g.

os.system("""osascript -e 'do shell script "mkdir -m 0775 -p \\"%s\\" " with administrator privileges'"""%d)

Hope that helps!

like image 127
Ben Avatar answered Mar 08 '23 10:03

Ben


You could run your entire script with admin privileges by making it into a native Mac OS X application using Platypus.

like image 26
svth Avatar answered Mar 08 '23 09:03

svth