Re: [Pyobjc-dev] PyObjC speech
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-05-18 17:17:01
|
On May 18, 2004, at 12:36 PM, Frederik De Bleser wrote: > Let's talk speech! > > I'm having trouble using the NSSpeechSynthesizer delegates. I believe > it's a problem with delegates in general. They seem to crash the > application. I'm posting the code here. The problem is when then speak > command is invoked. There are three reasons why it doesn't work: - BabbleBox really needed to be an NSObject subclass - You just made that signature up. Using incorrect signatures (which can also be no manually specified signature at all in the case of non-object arguments) will crash. How did you make that one up? I don't even think there is a 'z' type encoding... - It needs to be run from a NSApplication runloop to work properly, it's AppKit stuff > Can anybody please explain the correct way to use delegate methods in > PyObjC? Is it important to implement all elements of the delegate? And > is there anything on the net on the correct format for the signature? The correct way to use delegate methods in PyObjC is the same as in ObjC. It is not important to implement every method of the informal delegate protocol. The signature format is documented in Apple's "The Objective-C Programming Language" reference text, in the Type Encodings section of the Objective-C Runtime System chapter: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ 4objc_runtime_overview/chapter_4_section_6.html The following is a correct example.. if it doesn't work for you then you need to use a more recent version of PyObjC (perhaps even from CVS). import objc from Foundation import * from AppKit import * from PyObjCTools.AppHelper import installMachInterrupt class AppDelegate(NSObject): def applicationDidFinishLaunching_(self, app): self.synth = NSSpeechSynthesizer.alloc().init() self.synth.setVoice_("com.apple.speech.synthesis.voice.Agnes") self.synth.setDelegate_(self) self.speak_('hello there') def speak_(self, txt): self.synth.startSpeakingString_(txt) def speechSynthesizer_willSpeakWord_ofString_(self, synth, word, string): print "said:", string[word.location:word.location+word.length] speechSynthesizer_willSpeakWord_ofString_ = objc.selector(speechSynthesizer_willSpeakWord_ofString_, signature="v@:@{_NSRange=II}@") def speechSynthesizer_didFinishSpeaking_(self, synth, finishedSpeaking): if finishedSpeaking: print "finished speaking normally" else: print "finished speaking abnormally" NSApp().terminate_(self) if __name__ == '__main__': app = NSApplication.sharedApplication() delegate = AppDelegate.alloc().init() app.setDelegate_(delegate) # allow ctrl-c to work installMachInterrupt() app.run() |