Search code examples
applescriptapple-m1mediapipeosascript

How to change volume on mac using osascript and OpenCV


I'm using a 14 inch M1 Pro Macbook Pro, running Mac OS Monterey 12.6.

I'm making an OpenCV and Mediapipe based Computer Vision Project that allows me to use hand detection to control my Mac's volume. The code detects the distance between the tips of my index finger and thumb using the webcam, and changes the volume based on that. I've been trying to use osascript to set the volume:

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

It works, but only for hard coded values like 0, 5 and 10. How do I pass a variable value N to osascript:

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

If I could pass that variable value, then I could actually vary the volume instead of having it set at either 0, 5 or 10. The documentation hasn't been very helpful, anybody have any ideas or alternatives instead of osascript?

I've tried applescript but couldn't figure it out.


Solution

  • I'm guessing you are actually using Python, though you don't mention it in the question or in your tags.

    If so, you can use an "f-string" like this - note the f at the start:

    N = 7
    osascript.osascript(f"set volume output volume {N}")