Is it Possible to Use Python Code in Cocoa App?

Discussion in 'Mac Programming' started by Blakeasd, Feb 13, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello.
    I would like the following Python code to run when my user clicks a button linked with an IBAction.
    Here is the Python Code:

    Code:
    
    import pyaudio, array, math
    
    p = pyaudio.PyAudio()
    stream = p.open(rate=44100, channels=1, format=pyaudio.paFloat32, output=True)
    stream.write(array.array('f',
        (.25 * math.sin(i / 10.) for i in range(44100))).tostring())
    stream.close()
    p.terminate()
    
    
    Is it possible to have Python code inside an IBAction. If so, how?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    My version of Xcode doesn't seem to support the templates that should be used with PyObjc. Is there another way. I really need to use some Python code.
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    You don't need templates.
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    Cocoa is running on top of BSD Unix (tm, certified, etc.). So inside any Mac Cocoa app, you could always perform a *nix system() call to start a shell script to launch python (or any other interpreter) to run any text file with whatever source code you want inside it.
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    Initial disclaimer: I've never actually done this.

    If you don't want to or can't use PyObjC, you could embed Python into your Cocoa app.

    Call Py_Initialize() and Py_Finialize() in main. Create an Objetive-C method which is the direct target for the IBAction. Have the Objective-C method call some version of PyRun to run the Python code.

    More info on embedding Python is available from: docs.python.org/extending/embedding.html
     
  7. Blakeasd, Feb 13, 2011
    Last edited: Feb 13, 2011

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
  8. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #8
    Yes, you need the templates (or need to do the work there yourself), and the latest version of XCode does not include the templates. However, with a little bit of work you can get what you need form the PyObjC project: http://pyobjc.sourceforge.net/
     
  9. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    Does the pyobjc bridge work both ways? I only found examples of objective-c in a python program. Can you have python code inside an objective-c program. My program write now is written in objective-c but I'm just adding a new feature that is written in python.
     
  10. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #10
    Yes, objects can be instantiated and run in either direction.
     
  11. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #11
    Do you know of an example with Python code inside Objective-C I can look at?
     
  12. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #12
    Code:
    int errorStatus = system("/usr/bin/python /tmp/myscript.py");
    Or you can use a Unix pipe, or start a process and use a BSD socket. Objective C is a superset of ANSI C.
     
  13. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #13
    The software will be in the Mac app store so I can't make a directory on the users hard drive, so will the system command run files inside of my xcode project? If so, how?
     
  14. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    An app bundle is just a folder, so it should work fine. Not sure that the App Store rules allow it though.

    B
     
  15. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #15
    firewood, that is a gross misunderstanding of the poster's question. He is looking at using the scripting bridges, and that is a good usage of things. There is absolutely no need to shell out.

    Blakeasd: there is a lot of sample code on the PyObjC website, as well as documentation. You have gotten more than enough pointers leading you there. Please read what is there before asking more questions.
     

Share This Page