I am writing a morse code command line tool for Mac OS X written with Swift as a scripting language. I want to give the user the option to hear the morse code using Apple's NSSpeechSynthesizer. I can get this to work inside an app no sweat. But inside a command line tool or a Swift script the startSpeakingString() function is not audible--unless I step through the code line by line.
Here is the code in Swift (it's the same for a proper command line tool or a script)
import Foundation
import AppKit
var synth:NSSpeechSynthesizer = NSSpeechSynthesizer.init()
synth.startSpeakingString("dit dah")
And here is the code in an Objective-C command line tool
@import Foundation;
@import AppKit;
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSSpeechSynthesizer *synth = [[NSSpeechSynthesizer alloc] init];
[synth startSpeakingString:@"dit dah"];
}
return 0
}
The NSSpeechSythesizer instance seems legit in all cases. The startSpeakingString() function returns true in all cases. Here is my repo (work in progress): https://github.com/jpavley/swift-scripts
NSSpeechSynthesizer.isAnyApplicationSpeaking() waits until the speech is finished.
import Foundation
import AppKit
class Speaker: NSObject , NSSpeechSynthesizerDelegate {
var synth : NSSpeechSynthesizer!
func run() {
self.synth = NSSpeechSynthesizer.init()
self.synth.delegate = self
self.synth.startSpeakingString("First word")
while(NSSpeechSynthesizer.isAnyApplicationSpeaking() == true) {}
}
}
var speak : Speaker = Speaker.init()
speak.run()