Skip to content

Instantly share code, notes, and snippets.

@JashuaCovington
Last active November 29, 2016 20:31
Show Gist options
  • Save JashuaCovington/b226013b26d7fd5dd4123a28f7d7e24b to your computer and use it in GitHub Desktop.
Save JashuaCovington/b226013b26d7fd5dd4123a28f7d7e24b to your computer and use it in GitHub Desktop.
Swift Speech Box
//
// AppDelegate.swift
// iSpeach
//
// Created by Jashua Covington on 4/12/16.
// Copyright © 2016 Jashua Covington. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSSpeechSynthesizerDelegate {
@IBOutlet weak var window: NSWindow!
var multiWindow = [MultiWindowController()]
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
newMultiWindow(self)
}
func applicationWillTerminate(_ sender: Notification) {
// Insert code here to tear down your application
}
@IBAction func newMultiWindow(_ sender: AnyObject) {
let multiWindows = MultiWindowController()
multiWindow.append(multiWindows)
multiWindows.showWindow(self)
}
}
//
// iSpeachTests.swift
// iSpeachTests
//
// Created by Jashua Covington on 4/12/16.
// Copyright © 2016 Jashua Covington. All rights reserved.
//
import XCTest
@testable import iSpeach
class iSpeachTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
//
// iSpeachUITests.swift
// iSpeachUITests
//
// Created by Jashua Covington on 4/12/16.
// Copyright © 2016 Jashua Covington. All rights reserved.
//
import XCTest
class iSpeachUITests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
}
//
// MultiWindowController.swift
// iSpeach
//
// Created by Jashua Covington on 4/25/16.
// Copyright © 2016 Jashua Covington. All rights reserved.
//
import Cocoa
class MultiWindowController: NSWindowController,NSSpeechSynthesizerDelegate {
@IBOutlet weak var textField: NSTextFieldCell!
@IBOutlet weak var sayIt: NSButton!
@IBOutlet weak var stopIt: NSButton!
@IBOutlet weak var NSSpeechSynthesizerDelegate: NSButton!
let speechSynth: NSSpeechSynthesizer = NSSpeechSynthesizer()
var isTalking: Bool = false {
didSet {
updateButtons()
}
}
static var windowCounter:Int = 0
convenience init() {
self.init(windowNibName: "MultiWindowController")
speechSynth.delegate = self
}
override func windowDidLoad() {
super.windowDidLoad()
updateButtons()
speechSynth.delegate = self
window?.title = "Multi \(MultiWindowController.windowCounter)"
}
@IBAction func sayIt(_ sender: NSButton) {
let string = textField.stringValue
if string.isEmpty {
print("string from \(textField) is empty")
}else if isTalking == speechSynth.startSpeaking(string){
sayIt.isEnabled = true
}else {
sayIt.isEnabled = false
}
}
@IBAction func stopIt(_ sender: NSButton) {
speechSynth.stopSpeaking()
}
func updateButtons() {
if !isTalking {
sayIt.isEnabled = true
stopIt.isEnabled = true
} else if isTalking {
stopIt.isEnabled = false
sayIt.isEnabled = false
}
else{
sayIt.isEnabled = true
}
}
func speechSynthesizer(_ sender: NSSpeechSynthesizer, didFinishSpeaking finishedSpeaking: Bool) {
if !finishedSpeaking {
sayIt.isEnabled = false
}
else {
sayIt.isEnabled = true
}
}
func windowClose(_ sender: NSWindow) -> Bool {
if isTalking {
return !isTalking
} else {
return isTalking
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment