I’d like to have a python program alert me when it has completed its task by making a beep noise. Currently, I use import os and then use a command line speech program to say “Process complete”. I much rather it be a simple “bell.”
I know that there’s a function that can be used in Cocoa apps, NSBeep, but I don’t think that has much anything to do with this.
I’ve also tried
print(a)
but that didn’t work.
I’m using a Mac, if you couldn’t tell by my Cocoa comment, so that may help.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Have you tried :
import sys
sys.stdout.write('a')
sys.stdout.flush()
That works for me here on Mac OS 10.5
Actually, I think your original attempt works also with a little modification:
print('a')
(You just need the single quotes around the character sequence).
Method 2
If you have PyObjC (the Python – Objective-C bridge) installed or are running on OS X 10.5’s system python (which ships with PyObjC), you can do
from AppKit import NSBeep NSBeep()
to play the system alert.
Method 3
I tried the mixer from the pygame module, and it works fine. First install the module:
$ sudo apt-get install python-pygame
Then in the program, write this:
from pygame import mixer
mixer.init() #you must initialize the mixer
alert=mixer.Sound('bell.wav')
alert.play()
With pygame you have a lot of customization options, which you may additionally experiment with.
Method 4
I had to turn off the “Silence terminal bell” option in my active Terminal Profile in iTerm for print('a') to work. It seemed to work fine by default in Terminal.
You can also use the Mac module Carbon.Snd to play the system beep:
>>> import Carbon.Snd >>> Carbon.Snd.SysBeep(1) >>>
The Carbon modules don’t have any documentation, so I had to use help(Carbon.Snd) to see what functions were available. It seems to be a direct interface onto Carbon, so the docs on Apple Developer Connection probably help.
Method 5
Building on Barry Wark’s answer…
NSBeep() from AppKit works fine, but also makes the terminal/app icon in the taskbar jump.
A few extra lines with NSSound() avoids that and gives the opportunity to use another sound:
from AppKit import NSSound
#prepare sound:
sound = NSSound.alloc()
sound.initWithContentsOfFile_byReference_('/System/Library/Sounds/Ping.aiff', True)
#rewind and play whenever you need it:
sound.stop() #rewind
sound.play()
Standard sound files can be found via commandline locate /System/Library/Sounds/*.aiff
The file used by NSBeep() seems to be '/System/Library/Sounds/Funk.aiff'
Method 6
By the way: there is a module for that. 😉
Just install via pip:
pip3 install mac_alerts
run your sound:
from mac_alerts import alerts
alerts.play_error() # plays an error sound
Method 7
Play sound worked for me. Install using pip
pip3 install playsound
To play sound
from playsound import playsound
playsound('beep.wav')
References:
Found the examples here
downloaded beep.wav from here
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0