Shutdown Mac OSX computer using python

has has.temp3 at virgin.net
Mon Dec 8 18:31:57 EST 2008


On 8 Dec, 09:26, Håkan Hagenrud <hakan.hagen... at ikea.com> wrote:
> 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
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net




More information about the Python-list mailing list