Search code examples
pythonpython-3.xstringmacosvolume

How to use variable volume control for mac via python?


I found this code in python to control mac volume:

osascript.osascript("set volume output volume 35")

I want to use a variable to control this volume instead like:

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]))

I'm getting this error though:

TypeError: 'str' object is not callable

Is there any way I can input volume directly here?


Solution

  • you forgot the % after the string

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

    The C-style string format support number formatting: %d for integers, %f for floats, ... and further padding, formatting functionalities. See doc, PEP 3101