[英]How to get the list of open windows on MacOS in Swift?
Actually I'm trying to get the list of ALL open windows as next:实际上,我正在尝试获取所有打开的窗口列表,如下所示:
let options = CGWindowListOption(arrayLiteral: .excludeDesktopElements, .optionOnScreenOnly)
let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let infoList = windowsListInfo as NSArray? as? [[String: AnyObject]]
but the problem here is that I'm getting also Dock
or Window Server
, SystemUIServer
or widgets on the status bar.但这里的问题是我还在状态栏上收到了Dock
或Window Server
、 SystemUIServer
或小部件。 How can I improve my code to get avoid from those elements and to get only windows' list, as Xcode
, Finder
, etc.?如何改进我的代码以避免这些元素并仅获取 windows 列表,如Xcode
、 Finder
等?
It seems that all visible windows have the value 0
for key kCGWindowLayer
似乎所有可见窗口的键kCGWindowLayer
的值为0
import Cocoa
let options = CGWindowListOption(arrayLiteral: .excludeDesktopElements, .optionOnScreenOnly)
let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let infoList = windowsListInfo as! [[String:Any]]
let visibleWindows = infoList.filter{ $0["kCGWindowLayer"] as! Int == 0 }
print(visibleWindows)
public static func visibleWindows() -> [NSWindow]
{
return NSApplication.shared.windows.filter { $0.isVisible }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.