Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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?
 
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.
 
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.
 
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
 
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/
 
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.
 
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.

Yes, objects can be instantiated and run in either direction.
 
Do you know of an example with Python code inside Objective-C I can look at?
 
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?
 
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?

An app bundle is just a folder, so it should work fine. Not sure that the App Store rules allow it though.

B
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.