views:

2271

answers:

3

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.

Thanks!

+4  A: 

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).

gbc
+2  A: 

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.

markpasc
A: 

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.

Barry Wark