• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Does NSSound not work in a Unix tool? - SOLUTION
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does NSSound not work in a Unix tool? - SOLUTION


  • Subject: Re: Does NSSound not work in a Unix tool? - SOLUTION
  • From: Uli Zappe <email@hidden>
  • Date: Fri, 22 Mar 2002 19:37:37 +0100

Since I received a lot of emails in reaction to my code, and there seems to be quite some interest in really using this, here is a slightly improved version. It's only main.m that is a bit modified:

1.) It now uses sigsuspend() instead of pause() which is deprecated (thanks to Jonathan Feinberg <email@hidden> for pointing this out), and therefore doesn't need #import <unistd.h> anymore

2. If a sound cannot be found, it plays the system sound "Ping"; if even that isn't available for whatever reason, it simply returns. (The former version would wait endlessly for a Unix signal.)

Also note that while raising a SIGUSR1 signal would also terminate the program without a signal handling function defined via signal(), it would in this case print a respective error message in the Terminal instead of silently terminating OK.

Finally, I have removed [pool release]; which is superfluous since the program is terminating anyway. Does anybody know why Apple even uses it in its Unix Tool template? Is it just for aesthetic/educational value?



#############################################################
## ExitController.h
#############################################################

#import <Cocoa/Cocoa.h>

@interface ExitController : NSObject
{
}

- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool;

@end



#############################################################
## ExitController.m
#############################################################

#import "ExitController.h"

@implementation ExitController

- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool
{
raise(SIGUSR1);
}

@end



#############################################################
## main.m
#############################################################

#import <Cocoa/Cocoa.h>
#import "ExitController.h"

inline void sigexit(int i)
{
exit(0);
}

int main (int argc, const char * argv[])
{
NSSound *sound=nil;

signal(SIGUSR1, sigexit);
[[NSAutoreleasePool alloc] init];

if(argc>1) sound=[NSSound soundNamed:[NSString stringWithCString:argv[1]]];
if(!sound) sound=[NSSound soundNamed:@"Ping"];
if(!sound) return 1;
[sound setDelegate:[[ExitController alloc] init]];
[sound play];

sigsuspend(0);

return 1;
}

#############################################################


Bye
Uli
________________________________________________________

Uli Zappe email@hidden
Lorscher Stra_e 5 http://www.ritual.org
D-60489 Frankfurt Fon: +49-700-ULIZAPPE
Germany Fax: +49-700-ZAPPEFAX
________________________________________________________
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Does NSSound not work in a Unix tool? - SOLUTION
      • From: Andy Lee <email@hidden>
References: 
 >Re: Does NSSound not work in a Unix tool? - SOLUTION (From: Uli Zappe <email@hidden>)

  • Prev by Date: Simple newbie problem: Static variables in Objective-C?
  • Next by Date: Re: Open Recent menu in non-doc app
  • Previous by thread: Re: Does NSSound not work in a Unix tool? - SOLUTION
  • Next by thread: Re: Does NSSound not work in a Unix tool? - SOLUTION
  • Index(es):
    • Date
    • Thread