How to change volume with Python? - macos

I'm writing a program to wake me up mornings, but I want my program to play alarm sound as loud as its possible. so it needs to raise up volume to 100%. but I don't know how. I'm using python3 on macOS Sierra.

You can control the volume of your computer with Applescript:
set volume output volume 100
To execute Applescript from python you can use py-applescript which can be installed with sudo easy_install py-applescript. The following script will set the volume:
import applescript
applescript.AppleScript("set volume output volume 100").run()
EDIT: For Python3.6 you can use osascript instead: pip3.6 install osascript and:
import osascript
osascript.osascript("set volume output volume 100")

You do not need anything outside of the standard library to do this in python. Apple supports executing AppleScript from the terminal so the subprocess module is sufficient.
from subprocess import call
call(["osascript -e 'set volume output volume 100'"], shell=True)

Related

Raspberry Pi Motion on_motion_detected not running gphoto2 script

I'm trying to get Motion to trigger gphoto2 with on_motion_detected.
I can get this event to run a bash script, I can get it to run a python script. If either of those calls gphoto2 it fails.
Running the bash scrip or python script from the command line works. So the problem is probably something that is too in the weeds for me to figure out. Over on Reddit, a user with a similar problem found a solution by adding Motion as a user to the plugdev group. I tried that, but it didn't work for me.
https://www.reddit.com/r/raspberry_pi/comments/cqnuqh/help_gphoto2_and_motion_as_camera_trigger/
in motion.conf:
on_motion_detected /bin/bash /home/pi/Scripts/script.sh
and the script:
#!/bin/bash
gphoto2 --capture-image
Motion is using a Raspberry Pi Camera Module 2, and I'm trying to trigger a Sony A7Rii with gphoto2. Again, each works on its own separately, just not together.

Raspberry Pi pico script exeucte kill

I wrote a script with the while loop command and I executed it on my Raspberry Pi, however I didn't put a break in it, so it won't stop. I tried to interrupt it, but the script is still being executed. I used thonny Python and I tried to exit it, Ctrl+C, soft reboot. I even tried to put it in storage mode and reset it, but it's still being executed. I'm trying to work out how to completely delete the script from the pico itself. Do you have any ideas?
Python:
from machine import Pin
from time import sleep
led = Pin(25, Pin.OUT)
while True:
led.toggle()
sleep(0.5)
I'm reasonably sure that RPiOS has a task manager, so you could try killing it in that.
If it doesn't then open up your terminal and run ps -ef to find the PID of the python process (it should show the name of the command). Then run sudo kill -9 <PID> to kill it manually.
Edit:
I now understand that the RPi Pico is actually a microcontroller. Have you tried uploading a new program or just unplugging it from power to force it to reboot?

Run apple script after mac resumes from sleep mode

I want my macbook to run a script each time it resumes from sleep mode.
My code is extremely simple:
tell application "Finder"
if exists (disk "HDD") then
do shell script "diskutil eject HDD"
else
do shell script "diskutil mount HDD"
do shell script "diskutil eject HDD"
end if
end tell
I have a second HDD installed in the cd-rom bay and each time I resume from sleep mode I can hear the drive spinning even though it's current state is not mounted. But forcing a mount/eject command on the drive solves this issue. The only drawback is that I have to do this manually; which now I want to overcome.
Could someone help me on this matter?
There are several options in this page:
https://apple.stackexchange.com/questions/27036/possible-to-run-scripts-on-sleep-and-wake
It seems that the Scenario app could be of some help-

Computer Shut Off Python 3.4

A friend of mine wants to automatically shut off his computer using Python 3.4 (to put in a program). Does anyone know a basic as-small-as-possible way to do this?
OS:
Mac OSX Yosemite
Thanks
OS X is a Unix at its base, so you can use Unix commands. you can use either:
#!/usr/bin/python
import os
os.system("shutdown -h now")
Must be run as root, won't work otherwise. You can however add to sudoers (man visudo) shutdown as NOPASSWD program for the users that want to execute the script and use sudo shutdown … instead of just shutdown…
or
import subprocess
subprocess.call(['osascript', '-e',
'tell app "System Events" to shut down'])

Taking screen shot of current OS X or iTerm terminal window

In Linux, I'm using imagemagick import -window $WINDOWID to take a screencapture of the terminal.
Is there something similar in OS X?
You want to be using screencapture rather than import, which only works on X11 windows.
If the window is in front, you can try:
screencapture -l$(osascript -e 'tell app "Terminal" to id of window 1') test.png
See this question for some more background.
Try the tty command.
$ tty
/dev/ttys001
You can see what commands are running on that terminal by using `ps -t :
$ ps -t s001 #Where `tty` returns /dev/ttys001
You can also try pgrep too:
$ pgrep -t s001
Edit (You can/I can)
Oh, this is for a screen capture... Wasn't in the OP.
$WINDOWID is a X11 thing, and so is imagemagick. Linux GUI (both KDE and Gnome) are based upon the X11 protocol. The Mac GUI isn't.
On Mac OS X, there's a screencapture command. I haven't used it, but let's look at the manpage:
The screencapture utility is not very well documented to date.
You're on your own.
One more trick...
You can run the X11 server on the Mac. The server is no longer included in Mountain Lion, but Apple recommends you to install it from the XQuartz Project.
You can run the X11 server, then use XTerm windows which will have a Windows ID that can be used with imagemagick.
( Command + Control + Shift + 4 + Space ) for capturing a screenshot of the app window ..
Refer to this link: https://support.apple.com/en-us/HT201361 for learning more about capturing screenshots in macOS.. 👍

Resources