app.activateWithOptions() 和 app.activateWithOptions_() 之间的区别

Difference between app.activateWithOptions() and app.activateWithOptions_()

最近我使用 AppKit 编写了一个程序,旨在自动点击应用程序内部 window。当我想先激活 window 时,我浏览了 NSRunningApplication 文档,找到了一个名为 "activateWithOptions" 的函数,然后我写了一个简单的程序,如下所示。

Apps = NSWorkspace.sharedWorkspace().runningApplications()
for app in Apps:
    print(app.localizedName())
    app.activateWithOptions(NSApplicationActivateAllWindows)

这是我的问题。

  1. 第一个问题是,在文档中,localizedName 属性是一个变量,但在Python中,您必须将其用作函数才能获取名称。为什么不一样?
  2. 如果你运行程序,它只会在下面抛出一个错误。但是如果改成app.activateWithOptions_(NSApplicationActivateAllWindows),代码是可以通过的。为什么文档和我的用法不一致?

    AttributeError: 'NSRunningApplication' 对象没有属性 'activateWithOptions'

本地化名称

它不是一个变量,它是一个 属性 声明为:

@property(readonly, copy) NSString *localizedName;

合成到_localizedName实例变量和这个函数:

- (NSString *)localizedName {
  return _localizedName;
}

下划线

PyObjC 文档 - Underscores, and lots of them:

An Objective-C message looks like this:

[someObject doSomething:arg1 withSomethingElse:arg2];

The selector (message name) for the above snippet is this (note the colons):

doSomething:withSomethingElse:

In order to have a lossless and unambiguous translation between Objective-C messages and Python methods, the Python method name equivalent is simply the selector with colons replaced by underscores. Since each colon in an Objective-C selector is a placeholder for an argument, the number of underscores in the PyObjC-ified method name is the number of arguments that should be given.

The PyObjC translation of the above selector is (note the underscores):

doSomething_withSomethingElse_

activateWithOptions: -> activateWithOptions_