Shutdown Mac OSX computer using python

  • Thread starter Håkan Hagenrud
  • Start date
H

Håkan Hagenrud

Hello, i'm a python noob!

But I would like to shutdown a 10.5.x mac computer using python (2.5.1)

this is my code:
#!/usr/bin/python
import SystemEvents
down = SystemEvents.Power_Suite.Power_Suite_Events()
down.shut_down()

the last call needs an additional argument, I cant find any
documentation about this. Anybody knows?

Thank you!

Håkan Hagenrud
 
W

Wolfgang Draxinger

HÃ¥kan Hagenrud said:
Hello, i'm a python noob!

But I would like to shutdown a 10.5.x mac computer using python
(2.5.1)

this is my code:
#!/usr/bin/python
import SystemEvents
down = SystemEvents.Power_Suite.Power_Suite_Events()
down.shut_down()

the last call needs an additional argument, I cant find any
documentation about this. Anybody knows?

Thank you!

Why so complicated? MacOS X is a Unix at it's base, so you can
use Unix commands.

#!/usr/bin/python
import os
os.system("shutdown -h now")

Must be run as root, won't work otherwise. You can however add to
sudoers (man visudo) shutdown as NOPASSWD program for the users
that want to execute the script and use "sudo shutdown ..."
instead of just "shutdown ..."

Wolfgang Draxinger
 
H

has

Hello, i'm a python noob!

But I would like to shutdown a 10.5.x mac computer using python (2.5.1)

this is my code:
#!/usr/bin/python
import SystemEvents
down = SystemEvents.Power_Suite.Power_Suite_Events()
down.shut_down()

Avoid Python 2.x's ancient gensuitemodule/aetools modules - they've
always been flaky and/or broken on OS X and are completely hosed on
Intel Macs. For something this simple, you could just shell out to
osascript, avoiding any additional dependencies:

import subprocess
subprocess.call(['osascript', '-e',
'tell app "System Events" to shut down'])

(Note that I wouldn't recommend using 'shutdown' as Wolfgang suggests
- while it will shut down your system, it doesn't know anything about
the Mac desktop and will unceremoniously kill off all your GUI
processes without going through their normal quit procedures.)

HTH

has
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top