从 python 访问 API objc 常量和枚举
我正在使用 pyobjc 将一些所需的 OSX 功能添加到一些随机的 python 软件中。我需要从 python-land 访问 API 定义的 objc-land 常量。
此类常量的示例位于 NSRunningApplication 页面,具体是 NSApplicationActivationPolicy 的三个可能值。
对于上下文,当前手头的任务是列出当前正在运行的面向用户的应用程序。为此,以下代码工作得很好:
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
但我不想检查零(以使其更具可读性),也不想在我自己的代码中将虚拟 NSApplicationActivationPolicyRegular 值硬编码为零(当它在原始库中很容易获得时)。
如何通过 pyobjc 从 python 访问此类 objc 常量?
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 NSApplicationActivationPolicyRegular 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Apple 提供的 PyObjC 早于 10.6 中对 Cocoa 所做的一些添加。
NSRunningApplication
是这些添加内容之一,因此 PyObjC 不知道它。您需要将一些元数据添加到 AppKit BridgeSupport 文件中: /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport这三行将覆盖
您正在尝试使用的枚举
。请注意,像这样更改 PyObjC 可能意味着您必须静态链接并将更新的版本包含到您的应用程序中以进行分发,因为其他人计算机上的版本不会有此数据。最好编译最新版本的 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.bridgesupportThese three lines will cover the
enum
you're trying to use.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.