快速设置屏幕上所有窗口的大小和位置 [英] set the size and position of all windows on the screen in swift

查看:444
本文介绍了快速设置屏幕上所有窗口的大小和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以迅速获取所有应用程序的列表,并在前景中设置一个窗口,然后设置这些窗口的大小和位置.

Is it possible in swift to get a list of all apps with a window in the foreground and then set the size and position of these windows.

我得到了这样的Windows属性列表

I get the list of windows properties like this

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可访问性对象的AttributeValue会返回AXError(kAXErrorCannotComplete = -25204)

Now I try to change some of the propierties, but that has no effect. Also trying to get the AttributeValue for the TopLevel Accessability Object of PID returns AXError (kAXErrorCannotComplete = -25204)

推荐答案

感谢@Martin R的帮助

Got it thanks to the help of @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);
      }
    } 
  }
}

这篇关于快速设置屏幕上所有窗口的大小和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆