I am using pyobjc to add some needed OSX functionality to some random python software. I will need to access API-defined objc-land constants from python-land.
An example of such constants lies far down in the NSRunningApplication page, specifically the three possible values of NSApplicationActivationPolicy.
For context, the current task at hand is listing currently running, user-facing applications. To this end, the following code works just fine:
from Foundation import *
from Cocoa import *
import objc
for runningApp in sorted(
NSWorkspace.sharedWorkspace().runningApplications(),
key=lambda x: x.localizedName()
):
if runningApp.activationPolicy() == 0:
# Do stuff
But I'd rather not check against zero (to make it more readable) nor hardcode a dummy NSA开发者_StackOverflowpplicationActivationPolicyRegular value to zero in my own code when it's readily available in the original library.
How can I access such objc constants from python through pyobjc?
The Apple-supplied PyObjC predates some of the additions that were made to Cocoa in 10.6. NSRunningApplication
is one of these additions, and so PyObjC doesn't know about it. You need to add some metadata to the AppKit BridgeSupport file at: /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport
These three lines will cover the enum
you're trying to use.
<enum name='NSApplicationActivationPolicyRegular' value='0' />
<enum name='NSApplicationActivationPolicyAccessory' value='1' />
<enum name='NSApplicationActivationPolicyProhibited' value='2' />
Note that changing PyObjC like this probably means you'll have to statically link and include your updated version into your app for distribution, because the version on everyone else's machine won't have this data. It might be best to compile the newest version of PyObjC (which will contain these changes plus others) and use that.
精彩评论