从AXUIElementCopyAttributeValue中找到带有其windowNumber的窗口以更改其位置

时间:2019-05-03 21:41:28

标签: swift macos cocoa accessibility-api window-position

我想使用Accessibility API移动窗口。

带有以下代码段

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly, CGWindowListOption.optionOnScreenAboveWindow)
let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]

for entry  in windowList!
{ if let  bounds = entry[kCGWindowBounds as String] as? [String: Int],
     let owner = entry[kCGWindowOwnerName as String] as? String,
     let pid = entry[kCGWindowOwnerPID as String] as? Int32,
     let windowNumber = entry[kCGWindowNumber as String] as? Int32
  {  print("\(owner)  (\(pid)-\(windowNumber))")
     .....

检测到活动窗口。

对于某些进程(例如注释),我获得的条目具有相同的pid,但具有不同的windowNumber。 我想要的是移动具有给定pid和windowNumber的窗口。

我尝试了以下代码片段:

let appRef = AXUIElementCreateApplication(pid); 
var value: AnyObject?
let result = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &value)

if let windowList = value as? [AXUIElement]
{ ...

它返回进程窗口的列表。 我想用给定的windowNumber移动这些窗口之一(来自上面的片段)

如何找到该窗口?

0 个答案:

没有答案