0

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)
Hui Gordon
  • 345
  • 1
  • 3
  • 13
  • Probably not the answer you are expecting, but each process on macOS has `PID` you can use `os.kill` to achieve what you want if you know the `PID`. https://docs.python.org/3/library/os.html#os.kill – Gameplay Jan 17 '23 at 08:10
  • You could try with selenium – Vim Jan 17 '23 at 08:13
  • @Gameplay Thanks for your reply. Yes, actually, I only want to close specific windows, and `os.kill` kill the whole process. And different windows of the same application may share the same `PID`, which makes me unable to close a specific window with `os.kill`. – Hui Gordon Jan 17 '23 at 09:00
  • @Vim Sorry, I didn't make it clear that Google Chrome is just an example. I would also like to close the window of some other applications. – Hui Gordon Jan 17 '23 at 09:02

0 Answers0