cli.m 1001 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #import <AppKit/AppKit.h>
  2. @interface SpeechDelegate : NSObject <NSSpeechSynthesizerDelegate>
  3. @end
  4. @implementation SpeechDelegate
  5. - (void)speechSynthesizer: (NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)success {
  6. if (!success) {
  7. exit(-3);
  8. }
  9. exit(0);
  10. }
  11. @end
  12. int main(int argc, const char *argv[]) {
  13. if (argc < 2) {
  14. return -1;
  15. }
  16. // Init the speech engine
  17. NSSpeechSynthesizer *synth = [[NSSpeechSynthesizer alloc] init];
  18. [synth setDelegate: [SpeechDelegate new]];
  19. // Convert from c-string to NSString
  20. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  21. NSString *str = [NSString stringWithUTF8String: argv[1]];
  22. // Wait for other apps to stop speaking before we start
  23. // This provides no cordination with other waiting speakers
  24. while( [NSSpeechSynthesizer isAnyApplicationSpeaking] ) {
  25. [NSThread sleepForTimeInterval: 0.25];
  26. }
  27. // Speak
  28. [synth startSpeakingString: str];
  29. [[NSRunLoop currentRunLoop] run];
  30. // Cleanup (not reached)
  31. [pool drain];
  32. return 0;
  33. }