PlaySound OSX

For some reason there is no standard utility to play a sound file from a shell script. Well, here's one.

To use it, you just type

playsound foo.wav

You can download just the executable (Intel | PPC | ZIP file), or the source below. Place the file in /usr/local/bin, or wherever you feel comfortable with it. Such as inside your polish IM client :-)

Downlod this snippet as playsound.m

#import <Cocoa/Cocoa.h>

@interface Delegate : NSObject { }
@end

@implementation Delegate
- (void) sound: (NSSound *) sound didFinishPlaying: (BOOL) aBool
{
  [[NSApplication sharedApplication] terminate: nil];
}
@end


int main (int argc, char *argv[])
{
  if(argc!=2) {
    NSLog(@"usage: %s cowbell.wav", argv[0]);
    exit(1);
  }

  [[NSAutoreleasePool alloc] init];
  
  NSSound *sound = [[NSSound alloc]
		     initWithContentsOfFile: 
		       [NSString stringWithCString: argv[1]]
		     byReference: YES];
  
  [sound setDelegate: [[[Delegate alloc] init] autorelease]];
  [sound play];

  [[NSRunLoop currentRunLoop] run];

  return 1; // failed
}

Downlod this snippet as Makefile

test: playsound
	./playsound '/Applications/Mail.app/Contents/Resources/Mail Sent.aiff'
playsound: playsound.m Makefile
	gcc -o $@ -Wall -Os -framework Cocoa $<

[Comment on this]

Posted 2004 or so
How to enumerate bits quickly using DeBruijn sequences.
GCC has some clever tricks up its sleeve for compiling the humble division operator.
Some simple bite-sized browser enhancements. Drag them to your bookmarks toolbar.
These little hacks were originally posted at the Medallia blog; since I no longer work there, I thought it would be more appropriate to keep them here.
More...
Nov 27 2008
15:18 ui\
15:18 wef
Nov 28 2008
22:08 cool, random art, right?
22:08 seen better, though
Nov 29 2008
19:35 wtf :)
Dec 1 2008
14:12 Kom til Trondheim da...
15:10 hi
Dec 3 2008
14:52 пп
Dec 4 2008
03:56 blabb
04:58 bleep
Dec 5 2008
06:10 yah!
Dec 11 2008
02:38 irc+code
Dec 12 2008
15:22 Not a search box, sorry. And the WAP IRC is gone forever.
Dec 28 2008
20:02 shit
Dec 31 2008
23:09 test - bb
Jan 3 2009
17:11 nice site

My CSS-fu is weak; please use a recent browser.

Some rights reserved.

Random, semi-related background image by atomicshark.