我在 python 中找到这段代码来控制 mac 音量:
osascript.osascript("set volume output volume 35")
我想用一个变量来控制这个音量,而不是像:
volumes = [35, 35, 30, 35, 45, 45, 45, 45, 45, 40, 45, 45, 45, 45, 55, 45, 50, 45, 50, 55]
for i, n in enumerate(frequencies):
osascript.osascript("set volume output volume %s" (volumes[i]))
虽然我收到了这个错误:
TypeError: 'str' object is not callable
有什么方法可以直接在这里输入音量吗?
没关系,让它工作通过:
osascript.osascript("set volume output volume " + str(volumes[i]))
导入数学
进口操作系统
volume = 50 # 将这个数字设置在 0 和 100 之间
os.system('osascript -e "set Volume %d"' % math.ceil((8 * volume) / 100))