【问题标题】:set the size and position of all windows on the screen in swift快速设置屏幕上所有窗口的大小和位置
【发布时间】:2017-11-24 22:44:49
【问题描述】:

是否可以在 swift 中获取所有应用程序的列表,其中包含前台窗口,然后设置这些窗口的大小和位置。

我得到这样的 Windows 属性列表

let type = CGWindowListOption.optionOnScreenOnly
let windowList = CGWindowListCopyWindowInfo(type, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]

for entry  in windowList!
{

  var owner = entry[kCGWindowOwnerName as String] as! String
  var bounds = entry[kCGWindowBounds as String] as? [String: Int]
  var pid = entry[kCGWindowOwnerPID as String] as? Int32

  print ("\(owner)  \(bounds) \(pid)  ")

  if owner == "Erinnerungen"
  { bounds!["X"] = 0
    bounds!["Y"] = 0
    print("reset bounds")

    let appRef = AXUIElementCreateApplication(pid!);  //TopLevel Accessability Object of PID
    print(appRef)

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

    if result == .success, let windowList = value as? [AXUIElement]
    { // DO ANYTHING          
    } else
    { print("Result no Success or no valid windowlist returnd")          
    }
  }
}

现在我尝试更改一些属性,但这没有任何效果。 还尝试获取 PID 的 TopLevel Accessability 对象的 AttributeValue 返回 AXError (kAXErrorCannotComplete = -25204)

【问题讨论】:

  • 该错误的描述是:函数无法完成,因为消息传递以某种方式失败,或者因为与函数通信的应用程序繁忙或无响应。
  • 我尝试了 2 个系统应用程序终端和提醒。同时 kAXErrorCannotComplete = -25204。系统应用不应该支持可访问性吗?
  • 我不得不关闭沙箱,以避免这个错误

标签: swift macos cocoa


【解决方案1】:

感谢@Martin R 的帮助

let type = CGWindowListOption.optionOnScreenOnly
let windowList = CGWindowListCopyWindowInfo(type, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]

for entry  in windowList!
{
  let owner = entry[kCGWindowOwnerName as String] as! String
  var bounds = entry[kCGWindowBounds as String] as? [String: Int]
  let pid = entry[kCGWindowOwnerPID as String] as? Int32

  if owner == "Terminal"
  {
    let appRef = AXUIElementCreateApplication(pid!);  //TopLevel Accessability Object of PID

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

    if let windowList = value as? [AXUIElement]
    { print ("windowList #\(windowList)")
      if let window = windowList.first 
      {            
        var position : CFTypeRef
        var size : CFTypeRef
        var  newPoint = CGPoint(x: 0, y: 0)
        var newSize = CGSize(width: 800, height: 800)

        position = AXValueCreate(AXValueType(rawValue: kAXValueCGPointType)!,&newPoint)!;
        AXUIElementSetAttributeValue(windowList.first!, kAXPositionAttribute as CFString, position);

        size = AXValueCreate(AXValueType(rawValue: kAXValueCGSizeType)!,&newSize)!;
        AXUIElementSetAttributeValue(windowList.first!, kAXSizeAttribute as CFString, size);
      }
    } 
  }
}

【讨论】:

  • 谢谢!这是我发现的唯一一个真正适用于 swift 的例子。此外,它仅在系统首选项 -> 安全和隐私 -> 可访问性中启用 xcode 和 xcode 助手可访问性控制后才有效。还要按照@mica 的建议关闭应用程序功能部分下的沙箱
  • 给出错误Couldn't lookup symbols: _CGWindowListCopyWindowInfo
猜你喜欢
  • 1970-01-01
  • 2010-10-17
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 2021-11-19
  • 2016-12-11
相关资源
最近更新 更多