Skip to content

Instantly share code, notes, and snippets.

@kddlb
Created May 1, 2016 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kddlb/bb403df656e2daa78f43fd714ab303dd to your computer and use it in GitHub Desktop.
Save kddlb/bb403df656e2daa78f43fd714ab303dd to your computer and use it in GitHub Desktop.
Get all voices on an OS X system as a JSON
#!/usr/bin/swift
import Foundation
import Cocoa
var voices = NSSpeechSynthesizer.availableVoices();
var locale = NSLocale.init(localeIdentifier: "en-us");
var voicesInfo = [String:AnyObject]();
for voice in voices {
var currentVoice = NSSpeechSynthesizer.attributesForVoice(voice);
var cvInfo = [String:AnyObject]();
cvInfo["name"] = currentVoice[NSVoiceName]!;
cvInfo["gender"] = currentVoice[NSVoiceGender]!;
cvInfo["language"] = currentVoice[NSVoiceLocaleIdentifier]!;
cvInfo["languageName"] = locale.displayNameForKey(NSLocaleIdentifier, value: cvInfo["language"]!);
cvInfo["demoText"] = currentVoice[NSVoiceDemoText]!;
voicesInfo[currentVoice[NSVoiceIdentifier]! as! String] = cvInfo;
}
var data = try NSJSONSerialization.dataWithJSONObject(voicesInfo, options: NSJSONWritingOptions.PrettyPrinted);
var dataString = NSString.init(data: data, encoding: NSUTF8StringEncoding)
print(dataString!);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment