I didn't find a way to switch to a specific window yet, but you can switch to the app that contains a specific window using this function:
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)
}
}
It is probably usefull to switch by name as well:
func switchToApp(named windowOwnerName: String) {
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["kCGWindowOwnerName"] as? String) == windowOwnerName}), let pid = window["kCGWindowOwnerPID"] as? Int32 {
let app = NSRunningApplication(processIdentifier: pid)
app?.activate(options: .activateIgnoringOtherApps)
}
}
Example: switchToApp(named: "OpenOffice")
On my mac OpenOffice was started with a window with kCGWindowNumber = 599
, so this has the same effect: switchToApp(withWindow: 599)
As far as I found out so far, your options seem to be to show the currently active window of the app, or to show all windows (using .activateAllWindows
as activation option)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…