5

NOTE I changed the q slightly so that it is not a duplicate anymore. Sorry.

I have these time-consuming bioinformatics scripts I am running. I'd like them to sound a beep when they are done.

I am on OS X.

In a similar thread I found that print '\a' might work, but in Idle this just prints []

Why does this not work in IDLE

Community
  • 1
  • 1
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • [How to trigger from Python playing of a WAV or MP3 audio file on a Mac?](http://stackoverflow.com/questions/3498313/how-to-trigger-from-python-playing-of-a-wav-or-mp3-audio-file-on-a-mac) – TheZ Nov 09 '12 at 17:59
  • @TheZ: I think using `PyGame`, or even popening `afplay`, is probably overkill for someone looking for a system beep. – abarnert Nov 09 '12 at 18:01
  • @TheZ Thanks for the link, but I want to avoid having to play a sound-file if I can avoid it. – The Unfun Cat Nov 09 '12 at 18:01
  • @abarnert I was going off of the "I'd preferably like a slightly longer, more offensive beep than the OS X standard" section which sounds like it'd require an external sound. – TheZ Nov 09 '12 at 18:02
  • Possible duplicate of this question: http://stackoverflow.com/questions/13941/python-sound-bell. Does the second question work? – aychedee Nov 09 '12 at 18:02
  • @TheUnfunCat No worries, at least you'll have a fallback if nothing else works out :) – TheZ Nov 09 '12 at 18:02
  • @aychedee I think you are right. Sorry. I really did try searching well. – The Unfun Cat Nov 09 '12 at 18:07
  • @aychedee: The first answer there is the same as what the OP originally tried; the second is the same as my answer; the third is the Carbon equivalent (which will do the same thing as `NSBeep` in 32-bit, nothing at all in 64-bit); the fourth is @TheZ's suggestion. So, there's nothing new there. But I'm not sure whether this is a dup of that question, or the OP's real question is "Why isn't the `\a` solution working in Idle"? – abarnert Nov 09 '12 at 18:09
  • Changed my q so that hopefully something good can come of it. – The Unfun Cat Nov 09 '12 at 18:12
  • Thanks, you just made my answer look better; instead of clumsily going off on a tangent before answering the original question, it now directly answers the new question and then offers alternatives. If only I could write that way on purpose. :) – abarnert Nov 09 '12 at 18:20

2 Answers2

4

The reason it doesn't beep is that \a (or ^G) is the terminal bell code; it's up to the program handling stdout to turn that into a sound. Terminal.app will play a sound (unless you configure it to do "visual bell" instead, of turn it off entirely), but Idle will not. And, of course, if you're running without a tty, you get nothing.

If you don't mind using PyObjC (which comes pre-installed with the Apple-installed Pythons on all recent versions of OS X):

import Cocoa
Cocoa.NSBeep()

Of course this plays the OS X system beep, not the Terminal bell. Besides possibly being a different sound, this means if you disable the bell in Terminal, your script will still beep. (If you really want a Terminal bell, you can always script Terminal via, e.g., ScriptingBridge. But I don't think you care.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Appreciated, UV, but I would like to avoid importing modules – The Unfun Cat Nov 09 '12 at 18:04
  • 2
    You've got a bioinformatics script that works without importing any modules? No `math`, `itertools`, or even `sys`? At any rate, there are no function in the `__builtins__` namespace that will beep, so the terminal bell is your only option given that restriction, which means you will have to run in `Terminal.app` and leave the bell enabled. – abarnert Nov 09 '12 at 18:05
2

This tiny Python snippet using afplay does what I need: ten loud-ish dings at the end of a program:

from os import system
for i in range(0,10):
    system('afplay /System/Library/Sounds/Glass.aiff')

I presume the overhead of importing system is not small, but it works for me

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
dcnicholls
  • 391
  • 1
  • 5
  • 15