set the size and position of all windows on the screen in swift

前端 未结 1 907
醉梦人生
醉梦人生 2020-12-16 08:39

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.

I get the list of windows prop

相关标签:
1条回答
  • 2020-12-16 09:29

    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);
          }
        } 
      }
    }
    
    0 讨论(0)
提交回复
热议问题