如何在 OSX 中获取 NSStatusBar 的所有 NSStatusItem 元素?
我需要获取 OSX 状态栏中的所有元素。
我试图获取系统的 NSStatusBar id: [NSStatusBar systemStatusBar] 但我不知道如何获取其中的所有 NSStatusItems。 我在 NSStatusBar 中找到了一个名为 _items 的私有方法,但我无法调用它:
[[NSStatusBar systemStatusBar] _items];
Xcode 告诉我该方法不存在。
如何获取 NSStatusBar 中的所有 NSStatusItem 元素?
谢谢
I need get all elements in the status bar in OSX.
I tried to get the NSStatusBar id of the System: [NSStatusBar systemStatusBar] but I don't know how can I get all NSStatusItems in it.
I found a private method named _items in NSStatusBar but I can't call it:
[[NSStatusBar systemStatusBar] _items];
Xcode tould me that that method doesn't exist.
How can I get all NSStatusItem elements in the NSStatusBar?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法将所有项目作为
NSStatusItem
对象获取,因为它们并不全部属于您的进程。如果您只对它们在屏幕上的位置以及哪些应用程序拥有它们感兴趣,则可以使用 CGWindow API 来实现,因为从技术上讲,状态项是(无边框)窗口。下面是一个记录所有状态栏项目信息的示例:
请注意,不包括系统的项目;它们全部组合在一个属于“SystemUIServer”的窗口中。此外,此方法可能不是特别可靠,因为状态栏项目的窗口层可能会更改(此处假设为 25,但这没有在任何地方记录)。
You cannot get all items as
NSStatusItem
objects because they don't all belong to your process.If you're only interested where they are on screen and which apps own them, you can do that with the
CGWindow
APIs, because technically the status items are (borderless) windows. Here's an example that logs information about all status bar items:Note that the system's items are not included; they are all combined in one window that belongs to "SystemUIServer". Also, this method might not be particularly reliable because the window layer for status bar items might change (it's assumed to be 25 here, but this is not documented anywhere).
这是 Swift 中的更新片段:
尽管我认为最好将这些字典解压到一些更简单的 Swift 结构中。它有点样板,但它使调用代码更加简单。
调用代码就好多了:
Here's an updated snippet in Swift:
Though I think it's better to unpack these dictionaries into some simpler Swift structs. It's a bit more boilerplate, but it makes the calling code much simpler.
The calling code is then much nicer: