views:

194

answers:

1

I would like to adjust the system audio volume in OSX from a python script. This question about implementing keyboard shortcuts tells me how to do it in applescript, but I'd really like to do it from my python script without using os.system, popen, etc. Ideally I'd like to ramp up the volume slowly with some python code like this:

set_volume(0)
for i in range(50):
  set_volume(i*2)
  time.sleep(1)
+2  A: 

Use appscript to control the StandardAdditions scripting addition set volume command:

>>> from osax import *
>>> import time
>>> sa = OSAX()
>>> for i in range(50):
...   sa.set_volume(i*2)
...   time.sleep(1)
... 
>>> 
Ned Deily
Thanks, I'll give that a go.
Benson
I tried it, but instantiating OSAX on my machine fails with the following error: aem.ae.getappterminology isn't available in 64-bit processes.
Benson
Hmm, the example above works for me on 10.6.3 with the Apple-supplied Python 2.6.1 in 64-bit mode with Appscript 0.21.1 (`/usr/bin/easy_install-2.6 appscript`)
Ned Deily
I believe I'm using a macports-supplied python 2.6. I suppose I could try using apple's, but I'd really prefer to stick with macports. Perhaps I need to try recompiling package dependencies all in 64 bit mode or all in 32 bit mode.
Benson
The current py26-appscript version is 0.20.0. OSAX was fixed in a subsequent version to work in 64-bit mode. You can manually install the lastest appscript under MacPorts: `sudo port install py26-distribute; /opt/local/bin/easy_install-2.6 appscript` That's what I do.
Ned Deily
P.S. I've filed a MacPorts ticket requesting that the appscript ports be updated: http://trac.macports.org/ticket/24463
Ned Deily