1

I currently have a function that correctly positions the windows. But if I have 2 monitors and I want to position those windows on a second monitor it doesn't work, it always positions it on the first.

I already tried accessing windowList.last and windowList.first to see if anything changed and it doesn't work.

let type = CGWindowListOption.optionAll
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);
      }
    } 
  }
}

Set the size and position of windows on current or specific screen in Swift MacOS

0 Answers0