Questions tagged [pyobjc]

PyObjC is a bridge between the Python and Objective-C programming languages. It allows code written in either one of those languages to interact more or less directly with code written in the other. Its primary use is in the creation of software for Mac OS X. The PyObjC package includes wrappers for Apple's Objective-C frameworks, and most of their C language APIs. It also includes project templates and file templates for use with Apple's IDE, Xcode.

PyObjC's about page explains potential advantages the bridge offers to developers in either language. The bridge allows custom Objective-C and Python code to be co-mingled (nearly) painlessly, so that each can be used where its strengths are greatest.

The most obvious advantage for Python developers is that it allows them to write applications for Mac OS X with native appearance and behaviors. The use of Apple's frameworks facilitates the creation of the expected user experience, while familiar Python modules are always available when needed. A Python class can be a subclass of any framework class.

Objective-C users may find some of their models and application logic to be easier to express succinctly in Python. In particular, the setup and access of NSArray and NSDictionary objects can be cumbersome. The bridge makes a Python list or dictionary able to be used any place that its Cocoa counterpart is expected. Python's literal lists and dictionaries, list comprehensions, iteration, and dictionary lookup syntax make sometimes awkward Objective-C constructions into elegant and readable code. Python code that uses the bridge will also manage the memory of any Cocoa object it uses.

An excellent five-part tutorial, written by Will Larson, can be found on his website, titled "An Epic Introduction to PyObjC and Cocoa". Like the bridge itself, the tutorial serves more as an introduction for Python users to Cocoa than the reverse, but is nonetheless worth reading for anyone who wants to use PyObjC.

The PyObjC Introduction goes through the basics of using the bridge. Where syntax differs, the bridge tends towards making Python code more like Objective-C code. Two syntax differences are worth singling out here: method calls and instance variable access, which can cause confusion for users of the bridge.

Objective-C's message-call syntax uses positional arguments, but the name of the method is actually interleaved with the arguments:

// The name of this method is "actOnArg:usingOtherArg:andThisOneToo:"
[anInstance actOnArg:arg1 usingOtherArg:arg2 andThisOneToo:arg3];

It seems obvious, then, to use Python's keyword argument feature to emulate this:

// The name of this method is "actOnArg", and it has keyword arguments
anInstance.actOnArg(arg1, usingOtherArg=arg2, andThisOneToo=arg3) 

However, because keyword arguments ignore position, it is not feasible to make this kind of translation. To Python, the following call is equivalent to the preceding one:

anInstance.actOnArg(arg1, andThisOneToo=arg3, usingOtherArg=arg2)

while the Objective-C call:

// The name of this method is "actOnArg:andThisOneToo:usingAnotherArg:"
[anInstance actOnArg:arg1 andThisOneToo:arg3 usingOtherArg:arg2];

refers to an entirely different method than the original example.

PyObjC's solution is to use underscores in place of colons in method names:

anInstance.actOnArg_usingOtherArg_andThisOneToo_(arg1, arg2, arg3)

which unambiguously represents the name of the Objective-C method. This, unavoidably, is one of the main warts of the bridge -- the verbosity of Cocoa method names sometimes causes unwieldy Python calls:

NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1.0, self, objc.selector(self.actOnTimer_, signature='v@:@'), timerInfoDict, False)

As for instance variables, Objective-C has a separate namespace in each class for instance variables and method names, where Python has only one. A class in Objective-C can, and by convention does, have a method with the same name as an instance variable, which is a getter for that variable:

@interface MyObject : NSObject {
    int someIvar;
}
- (int)someIvar;
- (void)setSomeIvar:(int)newVal;   // The setter method for this ivar

Whereas this is impossible in Python. Python convention is to access instance variables directly, using the dot syntax:

anInstance.someIvar
anInstance.someIvar = 10

This especially causes confusion with the Objective-C 2.0 dot syntax, which is sugar for the standard accessor method:

anInstance.someIvar;         // Equivalent to [anInstance someIvar];
anInstance.someIvar = 10;    // Equivalent to [anInstance setSomeIvar:10];

PyObjC is forced to have its own convention, which is to give the ivar the name of the setter method, but prefixed with a single underscore. The convenience function objc.synthesize("someIvar") will create the ivar, the setter method, and the getter method with the expected names.

The PyObjC source is available and well worth a perusal; it especially repays investigation when getting into the trickier areas of the bridge, such as Objective-C methods which use out or plain-C parameters. In addition, the default Apple installation of PyObjC lags behind the latest version. On Snow Leopard particularly, this means that the bridge does not include some information needed to use new features of Apple's frameworks. (See "Problem with openPanelDidEnd" here on SO for an example.) That information is fortunately easily updated.

378 questions
0
votes
2 answers

NSWindow launched from statusItem menuItem does not appear as active window

I have a statusItem application written in PyObjC. The statusItem has a menuItem which is supposed to launch a new window when it is clicked: # Create statusItem statusItem =…
davidmytton
  • 38,604
  • 37
  • 87
  • 93
0
votes
1 answer

how to know which python version I have and how to switch between them and how to know which version has PyObjC

I have installed different versions of python during last year. when I open python interpreter, I can see which version I am currently running, But I want to know all the versions I have. I also want to switch between different python versions. I am…
brain storm
  • 30,124
  • 69
  • 225
  • 393
0
votes
1 answer

How do I add two phone numbers to the addressbook using pyObjc?

I have been trying to add both a "work" and a "home" phone number to the Mac AddressBook using python and pyObjC. I believe you would need add a multivalue object but I'm not sure how to do this using pyobjc. Here's the code sample I have put…
Denis Mayer
  • 45
  • 1
  • 5
0
votes
1 answer

python drag drop init issues

I'm confused as to how I would implement a drag and drop ability for a window and then have the url appear in the textbox. I've updated where I am stuck at class controller(NSWindow): #File to encode or decode form_file = IBOutlet() mainWindow =…
lostAstronaut
  • 1,331
  • 5
  • 20
  • 34
0
votes
1 answer

wxPython Mac OS X Fullscreen work around error

I was trying to get fullscreen support for a wxPython app using the code in the answer from this stackoverflow question wxPython MacOS X Lion full screen mode My Error Traceback (most recent call last): File "test_mac_fullscreen.py", line 36, in…
Zimm3r
  • 3,369
  • 5
  • 35
  • 53
0
votes
1 answer

How to color specific part of the text with some separator

I'm trying to color some specific part of the text, i have tried to say: if word.strip().startswith(":"): self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(word)) When someone types the sign : it gets colored green.…
0
votes
1 answer

Error installing PyObjC

Everytime I try to load PyObjC via the terminal in my Macbook Air (OS X 10.8.2) I get the following error. I am the only user on the Mac so what do I do? Davids-MacBook-Air:~ dave$ easy_install pyobjc==2.2 error: can't create or remove files in…
David Bailey
  • 603
  • 2
  • 7
  • 19
0
votes
2 answers

ImportError: No module named pyobjc

I am new to Python. I am running Mac OS X 10.8.2, Python 2.7.3, Xcode 4.5.1. I am not able to import pyobjc to python.I used easy_install pyobjc or manually downloading it from http://pypi.python.org/pypi/pyobjc/2.3 and running python setup.py…
VeilEclipse
  • 2,766
  • 9
  • 35
  • 53
0
votes
0 answers

OS X Python Script - How to launch it in the background?

Possible Duplicate: Running Python in background on OS X I have a nice python script that I would be able to launch in the background. I want it to be working on OS X. I have no ideas how to do that. I've found kind of a solution by launching…
Carto_
  • 577
  • 8
  • 28
0
votes
1 answer

Write a platform independent Python application with PyObjC

I'm writing a platform-independent Python application. For now it runs on Linux and OSX. However, I want to integrate platform-specific code. To be more precise I want to use the native notification systems (Growl, Mountain Lion's notification…
koloman
  • 711
  • 7
  • 19
0
votes
1 answer

Can't find output file with Python in Xcode

I just started learning Python about a week ago. I've made considerable progress thanks to everyone who has asked and answered questions. I came across a problem last night and after a lot of searching and pondering-- I still can't figure it out. I…
0
votes
1 answer

Leak when running PyObjC template in XCode 4

I downloaded the only Xcode PyObjC template I could find from: http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/ (the 'Cocoa-Python Core Data Document-based Application/'). When I ran it - without modifying, I get a leak where the…
janeh
  • 3,734
  • 7
  • 26
  • 43
-1
votes
2 answers

Developing tweaks for iPhone in Windows

I have searched all night and haven't found anything that I can use on this subject. I want to develop something for my iPhone. My only requirements are having this app always run in the background, and access the Location Services API on the…
glifchits
  • 683
  • 7
  • 26
-2
votes
2 answers

How to solve pyobjc installation problem?

pip install pyobjc-framework-Quartz Defaulting to user installation because normal site-packages is not writeable Collecting pyobjc-framework-Quartz Using cached pyobjc-framework-Quartz-6.2.2.tar.gz (3.4 MB) ERROR: Command errored out with…
-3
votes
2 answers

Equivalent Objective C syntax in python using pyobjc

Edit: To future googlers abarnert's answer works giving a NSHIObject (from HIToolbox, HI standing for human interface), I can find little more documentation on this, the only mention on apple.com (there is ZERO, ZIP, NADA documentation on a class…
Zimm3r
  • 3,369
  • 5
  • 35
  • 53
1 2 3
25
26