[Python] mac python , how to active an subprocess window

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to activate a subprocess window by python. First of all , run an app , click a button, then popup a new window (sub-processed). Using python to find the window by title, and activate it.

    from AppKit import NSWorkspace
    from Quartz import (CGWindowListCopyWindowInfo, kCGWindowListOptionOnScreenOnly, kCGNullWindowID)
    from Cocoa import NSApplicationActivateIgnoringOtherApps


    print('--- running applications ---')
    runningApps = NSWorkspace.sharedWorkspace().runningApplications()
    for app in runningApps:
    print(app)

    wndList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID)
    # print('----------> wndList')
    for wnd in wndList:
    # print(wnd)
    if wnd["kCGWindowName"] == "some title":
    print('--- found ---')
    print(wnd)
    # ---------here, how to activate the window?
    # follow code doesn't work, it's just active the main process app, not the subprocess window
    # app.unhide()
    # app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
    break


    using pstree tool can get

    -+= 00001 root /sbin/launchd
    \-+= 06960 /Applications/WeChat.app/Contents/MacOS/WeChat
    \-+- 06961 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app/C
    |--- 06964 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    |--- 06965 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    |--- 06967 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    |--- 06969 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    |--- 07085 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    |--- 12312 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    |--- 26736 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app
    \--- 29628 /Applications/WeChat.app/Contents/MacOS/WeChatAppEx.app


    The main process id is 06960 and the window's process id is 26736

    Continue reading...

Compartilhe esta Página