Actually, I wanna do some automated operations on macOS, like closing a specific window.
I have read some threads, and I got know about Appkit
and Quartz
from How can I minimize/maximize windows in macOS with the Cocoa API from a Python script?.
Here below is my current progress:
import AppKit
for app in AppKit.NSWorkspace.sharedWorkspace().runningApplications():
if app.localizedName() == 'Google Chrome':
app.hide()
With AppKit
, I can hide a specific application successfully. But there are two issues for me with this method: first, it seems that AppKit
can only manage Applications, but not Windows (i.e., the above code hides all Google Chrome windows at once); besides, AppKit
seems to be only able to Hide an application, but not Quitting it or Closing it.
I also tried Quartz
. With the below code, I can successfully find the specific windows that I wanna control, especially with the characteristic kCGWindowNumber
. But I would like to ask, is there any module that can allow me to close (or hide) the window, maybe like with the kCGWindowNumber
?
import Quartz
for window in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID):
if window['kCGWindowOwnerName'] == "Google Chrome" and window['kCGWindowLayer'] == 0:
print(window)