Sunday, April 22, 2012

PyObjC Templates for Xcode are back!

I came across a page from a guy who went to the trouble of making and posting new templates for using PyObjC with Xcode 4.

Thanks!

They are on github here. I just followed the instructions:

Copy the File Templates and Project Templates folders to the following path in your home directory, creating any missing intermediate directories if needed:

~/Library/Developer/Xcode/Templates/

I'm a little rusty with Xcode (and it's become more and more like flying the space shuttle or something. Still, I was able to recycle this old project, while changing it just a bit.


One funny thing, I couldn't figure out how to hook up the text field outlet in the old way. I had to use bindings, and of course I did so for the popup as well (both Content and Selected Index).

I don't have a lot of time to fool with Xcode any more, but I do want to check out Greg's blog. Thanks again.

Here's the AppDelegate:

#-*- coding: utf-8 -*-

from Foundation import *
from AppKit import *

class SpeakerAppDelegate(NSObject):
    TF = objc.ivar('TF')
    voiceL = objc.ivar('voiceL')
    nameL = objc.ivar('nameL')
    selectedIndex = objc.ivar('selectedIndex')
    notSpeaking =  objc.ivar('notSpeaking')
    pretext = 'Hi, my name is '
    
    def init(self):
        s = NSSpeechSynthesizer.alloc().initWithVoice_(None)
        self.speaker = s
        self.speaker.setDelegate_(self)
        self.setVoices()
        self.notSpeaking = True
        self.setSelectedIndex_(0)
        return self
    
    def setTF_(self, value):
        if value != self.TF:
            self.TF = value

    def setSelectedIndex_(self, value):
        if value != self.selectedIndex:
            self.selectedIndex = value
        name = self.nameL[self.selectedIndex]
        self.setTF_(self.pretext + name)

    def setVoices(self):
        L = NSSpeechSynthesizer.availableVoices()
        self.voiceL = L
        DL = [NSSpeechSynthesizer.attributesForVoice_(v) for v in L]
        nL = [D.objectForKey_('VoiceName') for D in DL]
        self.nameL = NSMutableArray.arrayWithArray_(nL)
        NSLog("%s" % self.nameL)
    
    @objc.IBAction
    def speak_(self,sender):
        i = self.selectedIndex
        NSLog("%i Say:  %s" % (i, self.TF))
        self.speaker.setVoice_(self.voiceL[i])
        self.notSpeaking = False
        self.speaker.startSpeakingString_(self.TF)
        pass
    
    def speechSynthesizer_didFinishSpeaking_(self,speechSyn,flag):
        NSLog("didFinishSpeaking_")
        self.notSpeaking = flag