0

I want to move a window with the Accessibility API.

With the following snippet

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))")
     .....

the active windows are detected.

For some processes (e.g. notes), I get entries with the same pid but a different windowNumber. What I want, is to move a window with a given pid and windowNumber.

I tried with this snippet:

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

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

It returns a list of the windows of a process. I want to move one of these windows with a given windowNumber (from the snippet above)

How do I find that window?

mica
  • 3,898
  • 4
  • 34
  • 62
  • 2
    What criteria are you using to pick the relevant window from the `CGWindowList`? Couldn't you apply those criteria to the Accessibility-provided windows, instead, to pick one of those windows directly (not based on its window number)? – Ken Thomases May 04 '19 at 00:48
  • I have a workaround using the window title and position to detect the desired window. Works OK, but I think the Windows-ID would be more precise. I can post the workaround, if someone needs it. – mica May 04 '19 at 08:25
  • How do you know the window ID? – Willeke May 04 '19 at 22:11
  • @mica please, post how did you get WindowNumber – Andrew_STOP_RU_WAR_IN_UA Dec 15 '19 at 02:15
  • @Andrew: I get the windowNumber as written above "let windowNumber = entry[kCGWindowNumber as String] as? Int32", but the solution doesn't work anymore, because I have problems getting the windowName in Catalina. – mica Dec 17 '19 at 08:52
  • @mica if you will find way to get window name/title for getting windowNumber... please, ping me. Thanks – Andrew_STOP_RU_WAR_IN_UA Dec 18 '19 at 08:44
  • @Andrew In Mojave this worked for me, to get the Windowtitel. let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as NSArray? as? [[String: AnyObject]] for entry in windowList! { let name = (entry[kCGWindowName as String] != nil) ? entry[kCGWindowName as String] as! String : "" As written before, that fails in Catalina, because I do not get the permission for Screen-Reading – mica Dec 18 '19 at 08:50

0 Answers0