I got it work (with help of this):
https://stackoverflow.com/questions/47152551/activate-a-window-using-its-window-id
You need to activate the window you select. Here the changes I made (I connected to a button for easier testing instead of tryToActivateWindow() ) ; I also changed the NSPoint, but that's a detail.
Here is the modified code:
func switchToApp(withWindow windowNumber: Int32) {
let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
guard let infoList = windowListInfo as NSArray? as? [[String: AnyObject]] else { return }
if let window = infoList.first(where: { ($0["kCGWindowNumber"] as? Int32) == windowNumber}), let pid = window["kCGWindowOwnerPID"] as? Int32 {
let app = NSRunningApplication(processIdentifier: pid)
app?.activate(options: .activateIgnoringOtherApps)
}
}
// }
//
// func tryToActivateWindow() {
@IBAction func tryToActivateWindow(_ sender: NSButton) {
print("begin -----------")
print(self.view.window?.windowNumber)
let mousePosition = NSPoint(x: 205,y: 200) // This corresponds to a visible window that's not part of this App
print("Mouse coordinates (\(mousePosition.x),\(mousePosition.y))")
let screenHeight = NSScreen.main!.frame.height
print("Screenheight: \(screenHeight)")
var screenPosition: NSPoint = mousePosition
screenPosition.y = screenHeight - mousePosition.y
print("Screen coordinates (\(screenPosition.x),\(screenPosition.y))")
let numberOfHitWindow = NSWindow.windowNumber(at: screenPosition, belowWindowWithWindowNumber: 0)
print("Window hit: \(numberOfHitWindow)")
CGDisplayMoveCursorToPoint(0, mousePosition) // Just for visual reference
print("Creating MouseDown event")
let newMouseDownEvent = NSEvent.mouseEvent(
with: .leftMouseDown,
location: mousePosition, //NSPoint(x: 205,y: 200), // this should be in the hitWindow
modifierFlags: NSEvent.ModifierFlags.control,
timestamp: ProcessInfo.processInfo.systemUptime,
windowNumber: numberOfHitWindow,
context: nil,
eventNumber: 0, //don't know what else to use
clickCount: 1,
pressure: 1)
self.perform(#selector(NSResponder.self.mouseDown(with:)), with: newMouseDownEvent)
print("Sleeping for a bit")
Thread.sleep(forTimeInterval: 0.1)
print("Woke up")
print("Creating MouseUp event")
let newMouseUpEvent = NSEvent.mouseEvent(
with: .leftMouseUp,
location: mousePosition, //NSPoint(x: 205,y: 200), // this should be in the hitWindow
modifierFlags: NSEvent.ModifierFlags.control,
timestamp: ProcessInfo.processInfo.systemUptime,
windowNumber: numberOfHitWindow,
context: nil,
eventNumber: 1, //don't know what else to use
clickCount: 1,
pressure: 1)
self.perform(#selector(NSResponder.self.mouseUp(with:)), with: newMouseUpEvent)
print("end -----------")
switchToApp(withWindow: Int32(numberOfHitWindow)) // <---- THAT'S THE KEY POINT
}
As for counter, 0 and 1 work:
https://stackoverflow.com/questions/2662128/how-to-get-the-current-eventnumber-for-creating-an-event-with-nsevent
For your other question:
In a related question: how does one emulate a double-click?
Just pas 2 as a value for clickCount in newMouseDownEvent.
If that works, don't forget to close the thread on the correct answer. Good luck.