How to change volume on mac using osascript and OpenCV - applescript

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

Related

Bash script does not recognize device id?

I've been working on a script that will run on startup but I am running into a problem. The script is meant to slow mouse acceleration because I use a gaming mouse and it's always too fast.
When I use xinput --list I get this output (out of many other lines):
SteelSeries Sensei Raw Gaming Mouse id=10 [slave pointer (2)]
When I open terminal and run this command, everything runs fine and my sensitivity is changed:
xinput --set-prop 10 "Device Accel Constant Deceleration" 2
However, when I put the above string in a shell.sh with 'eval' in the beginning, it prompts me the following error:
property 'Device' doesn't exist, you need to specify its type and
format
What am I doing wrong?
You don't need eval; put the command exactly as you used it from the terminal in your script.
The problem is that eval essentially reparses the string it receives, which results from joining the arguments arguments with whitespace. Your eval command is equivalent to
xinput --set-prop 10 Device Accel Constant Deceleration 2
You could use eval if you quoted the entire string:
eval 'xinput --set-prop 10 "Device Accel Constant Deceleration" 2'
but there is no reason to do so; just use
xinput --set-prop 10 "Device Accel Constant Deceleration" 2
As said on other posts, you don't need to use eval. I would also add something else: you should use the name of the device, rather than the id number, because the id number can change if you add things to your computer (or for other more obscure reasons, too). I would recommend this:
xinput --set-prop "SteelSeries Sensei Raw Gaming Mouse" "Device Accel Constant Deceleration" 2

FMX Change and mute master volume in OS X

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.

NodeJS: Unable to write to Windows 8 block device

I'm looking forward to write directly to a raw windows block device.
I can successfully do so with dd for windows:
> dd.exe if=myData.dat of=\\.\PhysicalDrive1
However I'm unable to do so using NodeJS. I'm using node-blockdevice in the following manner:
var device = new BlockDevice({
path: '\\\\.\\PhysicalDrive1',
mode: 'w+',
size: 512
});
device.write(0, myBuffer, callback);
device.write correctly returns the amount of bytes written, however it's not actually writing anything to the device.
Notice that the exact code works successfully in Mac OS X (substituting \\\\.\\PhysicalDrive1 with /dev/diskN of course): it writes my data and I can view it without any problem in Windows 8.
What am I doing wrong?
I also tried:
Not escaping the backslashes (\\.\PhysicalDrive1) but that results in a EINV error.
Using the logical name: \\\\.\\E:.
Unmounting the volume with mountvol X: /D before attempting to read/write.
I can correctly confirm the id of the device I want to write to with:
wmic diskdrive list brief
I've also tried setting the mode to rs+. The read operation seemed to work, however the saved data contained the following failure-related data:
�X�MSDOS5.0�
�?����:�)?�xNO NAME FAT32 3ɎѼ�{��ٽ|�V#�N�V#�A��U�r��U�u
��t�F�-�V#�s�����f��#f������Af��f��f�F�~u9�~*w3f�Ff��
����,���}��|���t<�t ������}��}��ߘ��f`�~� fjfPSfh�B�V#���fXfXfXfX�3f;F�r��*f3�f�Nf����f��f���v�֊V#����
̸�fa�t���f#Iu��BOOTMGR
Disk error�
Press any key to restart
��U�%
EDIT: A github issue thread describing more things I've tried: https://github.com/jhermsmeier/node-blockdevice/issues/1.
EDIT: All approaches mentioned were tested with admin privileges.
EDIT: I'm using device.close(callback), but omitted in the example for simplicity.

How to identify PC (motherboard) in win32 api? [duplicate]

How to uniquely identify computer (mainboard) using C#(.Net/Mono, local application)?
Edition. We can identify mainboard in .Net using something like this (see Get Unique System Identifiers in C#):
using System.Management;
...
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_MotherboardDevice");
...
But unfortunately Mono does not support System.Management. How to do it under Mono for Linux? - I don't know :(
Write a function that takes a few unique hardware parameters as input and generates a hash out of them.
For example, Windows activation looks at the following hardware characteristics:
Display Adapter
SCSI Adapter
IDE Adapter (effectively the motherboard)
Network Adapter (NIC) and its MAC Address
RAM Amount Range (i.e., 0-64mb, 64-128mb, etc.)
Processor Type
Processor Serial Number
Hard Drive Device
Hard Drive Volume Serial Number (VSN)
CD-ROM / CD-RW / DVD-ROM
You can pick up a few of them to generate your unique computer identifier.
Please see: Get Unique System Identifiers in C#
You realistically have MotherboardID, CPUID, Disk Serial and MAC address, from experience none of them are 100%.
Our stats show
Disk serial Is missing 0.1 %
MAC Is missing 1.3 %
Motherboard ID Is missing 30 %
CPUID Is missing 99 %
0.04% of machines tested yielded no information, we couldn't even read the computer name. It maybe that these were some kind of virtual PC, HyperV or VMWare instance, or maybe just very locked down? In any case your design has to be able to cope with these cases.
Disk serial is the most reliable, but easy to change, mac can be changed and depending on the filtering applied when reading it can change if device drivers are added (hyperv, wireshark etc).
Motherboard and CPUID sometimes return values that are invalid "NONE", "AAAA..", "XXXX..." etc.
You should also note that these functions can be very slow to call (they may take a few seconds even on a fast PC), so it may be worth kicking them off on a background thread as early as possible, you ideally don't want to be blocking on them.
Try this:
http://carso-owen.blogspot.com/2007/02/how-to-get-my-motherboard-serial-number.html
Personally though, I'd go with hard drive serial number. If a mainboard dies and is replaced, that PC isn't valid any more. If the HDD drive is replaced, it doesn't matter too much because the software was on it.
Of course, on the other hand, if the HDD is just moved elsewhere, the information goes with it, so you might want to look at a combination of serial numbers, depending what you want it for.
How about the MAC address of the network card?

Display (mntr) DeviceID Mac OS X in Bash?

I need to query the DeviceID of each connected monitor on Mac OS X Leopard & Snow Leopard. is this possible to do using bash? if not what would be the best approach?
would this be stored in a preference file anywhere?
i tried accessing the system_profiler info but it does not look like the device id is included for the monitors.
any help would be greatly appreciated...
Thanks!
You can find here How to Get the Display Name with the Display ID in Mac OS X? one small C program. You can compile it, and when you run it will show Device ID.
For example, for my notebook will return:
Color LCD : 69677760
it is decimal number, when you convert it to hexadecimal
echo 69677760 16 o p | dc
will return the 42732C0 hex-number what is the last part of Device ID from the colorsync.

Resources