2

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)
Community
  • 1
  • 1
Benson
  • 22,457
  • 2
  • 40
  • 49
  • 1
    Related question: How to programmatically set volume in Windows, Mac and Ubuntu? http://stackoverflow.com/questions/1920749/how-to-programmatically-set-volume-in-windows-mac-and-ubuntu – jfs Apr 02 '10 at 06:10

1 Answers1

1

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
  • 83,389
  • 16
  • 128
  • 151
  • 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 Apr 03 '10 at 22:25
  • 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 Apr 04 '10 at 00:39
  • 1
    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 Apr 13 '10 at 05:55
  • 1
    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 Apr 13 '10 at 07:04
  • 1
    P.S. I've filed a MacPorts ticket requesting that the appscript ports be updated: http://trac.macports.org/ticket/24463 – Ned Deily Apr 13 '10 at 07:47
  • For those of you who who stumble upon this long after-the-fact, you may be interested to know that **appscript development and support has ended**. From [the appscript website](http://appscript.sourceforge.net/status.html): "Appscript development and support ended in 2012 as a reaction to Apple's long-time mismanagement of Mac Automation and consequent decay of those technologies and ecosystem. Use at own risk." – stephmur Jun 03 '19 at 15:20