ronaldoussoren / pyobjc

The Python <-> Objective-C Bridge with bindings for macOS frameworks
https://pyobjc.readthedocs.io
563 stars 47 forks source link

AppKit: Can't get list of windows for a given NSApplication instance #599

Closed trevorstr closed 6 months ago

trevorstr commented 7 months ago

Describe the bug

I am trying to obtain a list of windows for an NSApplication instance. However, the windows() variable seems to be missing from the NSApplication class in PyObjC.

from AppKit import NSWorkspace

app_list = NSWorkspace.sharedWorkspace().runningApplications()

for app in app_list:
    if app.bundleIdentifier() == 'com.google.Chrome':
        print(app.windows())
AttributeError: 'NSRunningApplication' object has no attribute 'windows'

Platform information

To Reproduce

See script above

Expected behavior

According to the documentation for AppKit, there's supposed to be a windows variable exposed from the NSApplication class, which returns an array of windows for the application.

https://developer.apple.com/documentation/appkit/nsapplication/app_windows

mrpippy commented 7 months ago

NSWorkspace.sharedWorkspace().runningApplications() returns an array of NSRunningApplication objects, not NSApplication. If you want to get the list of open windows in another app, you'll likely need to use AppleScript/Apple Events.

ronaldoussoren commented 7 months ago

NSWorkspace.sharedWorkspace().runningApplications() returns an array of NSRunningApplication objects, not NSApplication. If you want to get the list of open windows in another app, you'll likely need to use AppleScript/Apple Events.

Indeed. The ScriptingBridge framework is a fairly easy way to interact with AppleScript/AppleEvents, and you can use the Script Editor application to check what applications support.