问题
I'm writing a program to wake me up mornings, but I want my program to play alarm sound as loud as its possible. so it needs to raise up volume to 100%. but I don't know how. I'm using python3 on macOS Sierra.
回答1:
You can control the volume of your computer with Applescript:
set volume output volume 100
To execute Applescript from python you can use py-applescript which can be installed with sudo easy_install py-applescript. The following script will set the volume:
import applescript
applescript.AppleScript("set volume output volume 100").run()
EDIT: For Python3.6 you can use osascript instead: pip3.6 install osascript and:
import osascript
osascript.osascript("set volume output volume 100")
回答2:
You do not need anything outside of the standard library to do this in python. Apple supports executing AppleScript from the terminal so the subprocess module is sufficient.
from subprocess import call
call(["osascript -e 'set volume output volume 100'"], shell=True)
来源:https://stackoverflow.com/questions/45772011/how-to-change-volume-with-python