如何通过 python 使用 mac 的可变音量控制?

问题描述 投票:0回答:3

我在 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

有什么方法可以直接在这里输入音量吗?

python python-3.x string macos volume
3个回答
0
投票

你忘记了字符串后的

%

"set volume output volume %s" % volumes[i]

C风格的字符串格式支持数字格式:

%d
用于整数,
%f
用于浮点数,...以及进一步的填充、格式化功能。见docPEP 3101


0
投票

没关系,让它工作通过:

osascript.osascript("set volume output volume " + str(volumes[i]))

0
投票

导入数学

进口操作系统

volume = 50 # 将这个数字设置在 0 和 100 之间

os.system('osascript -e "set Volume %d"' % math.ceil((8 * volume) / 100))

© www.soinside.com 2019 - 2024. All rights reserved.