I have been trying to use tell application "Google Chrome" to set sound volume to 5 but it gives me error "The variable volume is not defined." number -2753 from "volume". How do I do this?
Chrome doesn't have an application-specific volume setting. You can set the volume for your system with set volume output volume 50, where you can use any integer from 0 to 100.
Related
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.
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}")
Use command
osascript -e "set Volume 10"
or use python library driver_controler https://github.com/Alexandro1112/driver-controller
I have the following to identify if a volume is mounted:
on volumeMounted:aNotification
set volumeName to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
if volumeName is "myVolume" then
--do something
end if
end volumeMounted:
on volumeUnmounted:aNotification
set volumeName to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
if volumeName is "myVolume" then
--do something
end if
end volumeUnmounted:
I wanted a way to identify if an external volume is mounted even though it has the same name.
For example:
my internal volume is named "Macintosh HD" and I connect an external volume with the same name
it is only recognizing 1 volume, so I can't unmount both volumes with a Button created in the Interface.
Note: I know I could just right click and ask to eject but I wanted to do that in the UI.
There is another key NSWorkspaceVolumeURLKey in the userInfo dictionary of the volumeMounted notification .
The associated value is the URL of the mount point. You can cast the URL to alias. The default folder for mounted volumes is /Volumes, the path of the startup volume is always /.
I wrote an FMX (FireMonkey) application and I want to change (increase / decrease) and mute / unmute the master volume output in OS X. Either in Delphi or C++Builder. Alternatively I would do it by simulating key presses of the specific keys of the keyboard.
For Windows, it is fairly easily by simulating key presses with SendInput() or even easier with keybd_event().
This is how it works on windows for me:
// vkVolumeUp / vkVolumeDown / vkVolumeMute
// VK_VOLUME_UP / VK_VOLUME_DOWN / VK_VOLUME_MUTE
keybd_event(vkVolumeUp, 1, 0, 0);
keybd_event(vkVolumeUp, 1, KEYEVENTF_KEYUP, 0);
But I can't manage to compile it for OS X, since the IDE tells me that it doesn't know this functions. A direct way to change the volume would be even better if it is possible.
This is probably a long-winded, inefficient way of doing it, but you can mute the volume from the Terminal like this:
osascript -e 'set volume with output muted'
and increase it by 20 notches like this
osascript -e 'set volume output volume ((output volume of (get volume settings)) + 20)'
I presume you could use the system() command to execute those till someone tells you a better way.
I need to rename and fill about 70 USB sticks. Having an Applescript run automatically when they are inserted would make it much simpler.
Any external drive you connect to an OS X machine is mounted into /Volumes. If you watch that folder for changes, you can pick up on added external drives and process these. Running this code:
property ignoredVolumes : {"Macintosh HD", "Time Machine Backups"} -- add as needed
tell application "System Events"
set rootVolume to disk item (POSIX file "/Volumes" as text)
set allVolumes to name of every disk item of rootVolume
repeat with aVolume in allVolumes
if aVolume is not in ignoredVolumes then
set name of disk item (path of rootVolume & aVolume) to newName
end if
end repeat
end tell
will rename drives that are not in your ignoredVolumes list (unplug all but those you want to ignore, run ls /Volumes in Terminal and add the names to the property) to newName. To have this triggered on every change, modify the codes to be a Stay-Open script application:
property pollIntervall : 60 -- in seconds
property ignoredVolumes : {…} -- from above
on run
my checkVolumes()
end run
on idle
my checkVolumes()
return pollInterval
end idle
on checkVolumes()
tell … end tell -- from above
end checkVolumes
and save it in AppleScript Editor (select “AppleScript application”, make sure you tick “Stay Open” when you do). Once launched, the script will keep running, executing the on idle handler every pollInterval seconds.
This will do fine if you are basically doing a once-in-a-while batch job. If you want a more permanent solution that does not rely on running a stay-open script application, you can either
attach a Folder Action Script to the /Volumes folder (hat tip to Philip Regan on Ask Different; details on how to configure the action in this Mac OS X Hints post) – the advantage being you strictly process additions to /Volumes, or
use launchd by creating a LaunchAgent with the StartOnMount key set to true – that will trigger the script / app the agent starts every time a filesystem is mounted (tip of the hat to Daniel Beck at Ask Different; see Apple’s Technical Note TN2083 for the gory details).
I found the command, but can I do it in AppleScript?
Yes, you can: use set volume output volume N, where N is an integer from 0 to 100. Since there are 16 squares in the volume interface, and 100/16 = 6.25, there's no direct map from squares to this number, but you'll be fine if you think about it as a percentage. There's also the ability to set the input volume with set volume input volume N, and the alert volume with set volume alert volume N; set volume output muted BOOL mutes the output if BOOL is true, and unmutes it if it's false. (Alternatively, set volume ... with output muted or set volume ... without output muted.) You can stack these if you want to set multiple things at once. As Adam Rosenfield says, there's also set volume R, which (according to my docs) takes a real number between 0 and 7; however, this is deprecated, and has a strange range; I'd just use set volume output volume instead. If you want to query the current volume, you can run get volume settings, which returns a record of the form {output volume:82, input volume:46, alert volume:100, output muted:false}.