calling Objective C functions from Python?

前端 未结 4 1719
刺人心
刺人心 2020-12-14 04:19

Is there a way to dynamically call an Objective C function from Python?

For example, On the mac I would like to call this Objective C function

[NSSpe         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 05:01

    Since OS X 10.5, OS X has shipped with the PyObjC bridge, a Python-Objective-C bridge. It uses the BridgeSupport framework to map Objective-C frameworks to Python. Unlike, MacRuby, PyObjC is a classical bridge--there is a proxy object on the python side for each ObjC object and visa versa. The bridge is pretty seamless, however, and its possible to write entire apps in PyObjC (Xcode has some basic PyObjC support, and you can download the app and file templates for Xcode from the PyObjC SVN at the above link). Many folks use it for utilities or for app-scripting/plugins. Apple's developer site also has an introduction to developing Cocoa applications with Python via PyObjC which is slightly out of date, but may be a good overview for you.

    In your case, the following code will call [NSSpeechSynthesizer availableVoices]:

    from AppKit import NSSpeechSynthesizer
    
    NSSpeechSynthesizer.availableVoices()
    

    which returns

    (
        "com.apple.speech.synthesis.voice.Agnes",
        "com.apple.speech.synthesis.voice.Albert",
        "com.apple.speech.synthesis.voice.Alex",
        "com.apple.speech.synthesis.voice.BadNews",
        "com.apple.speech.synthesis.voice.Bahh",
        "com.apple.speech.synthesis.voice.Bells",
        "com.apple.speech.synthesis.voice.Boing",
        "com.apple.speech.synthesis.voice.Bruce",
        "com.apple.speech.synthesis.voice.Bubbles",
        "com.apple.speech.synthesis.voice.Cellos",
        "com.apple.speech.synthesis.voice.Deranged",
        "com.apple.speech.synthesis.voice.Fred",
        "com.apple.speech.synthesis.voice.GoodNews",
        "com.apple.speech.synthesis.voice.Hysterical",
        "com.apple.speech.synthesis.voice.Junior",
        "com.apple.speech.synthesis.voice.Kathy",
        "com.apple.speech.synthesis.voice.Organ",
        "com.apple.speech.synthesis.voice.Princess",
        "com.apple.speech.synthesis.voice.Ralph",
        "com.apple.speech.synthesis.voice.Trinoids",
        "com.apple.speech.synthesis.voice.Vicki",
        "com.apple.speech.synthesis.voice.Victoria",
        "com.apple.speech.synthesis.voice.Whisper",
        "com.apple.speech.synthesis.voice.Zarvox"
    )
    

    (a bridged NSCFArray) on my SL machine.

提交回复
热议问题