34.7 C
Colombia
domingo, julio 6, 2025

macos: cómo determinar cuál proceso posee una ventana en specific


Este script imprimirá información (poseyendo PID) sobre las ventanas visibles.
E información de la ventana que cambió de posición en un intervalo de 5 segundos.
El crédito va a respuesta de superusuario

#!/usr/bin/env python
    
import Quartz
import time
from Basis import NSSet, NSMutableSet
def transformWindowData(information):
    list1 = []
    for v in information:
        if not v.valueForKey_('kCGWindowIsOnscreen'):
            proceed
        

        row = ( 
            str(v.valueForKey_('kCGWindowOwnerPID') or '?').rjust(7) + 
            ' ' + str(v.valueForKey_('kCGWindowNumber') or '?').rjust(5) + 
            ' {' + ('' if v.valueForKey_('kCGWindowBounds') is None else 
                ( 
                    str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('X')))     + ',' + 
                    str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Y')))     + ',' + 
                    str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Width'))) + ',' + 
                    str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Top'))) 
                ) 
                ).ljust(21) + 
            '}' + 
            't[' + ((v.valueForKey_('kCGWindowOwnerName') or '') + ']') + 
            ('' if v.valueForKey_('kCGWindowName') is None else (' ' + v.valueForKey_('kCGWindowName') or '')) 
        ).encode('utf8')
        list1.append(row)

    return list1;

def printBeautifully(dataSet):
    print 'PID'.rjust(7) + ' ' + 'WinID'.rjust(5) + '  ' + 'x,y,w,h'.ljust(21) + ' ' + 't[Title] SubTitle'
    print '-'.rjust(7,'-') + ' ' + '-'.rjust(5,'-') + '  ' + '-'.ljust(21,'-') + ' ' + 't-------------------------------------------'

    # print textList1
    for v in dataSet:
        print v;

#seize preliminary set
wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID)
wl = sorted(wl, key=lambda okay: okay.valueForKey_('kCGWindowOwnerPID'))

#convert into readable format
textList1 = transformWindowData(wl);

#print every part now we have on the display screen
print 'all home windows:'
printBeautifully(textList1)

print 'Transfer goal window'
time.sleep(5)

#seize window information the second time
wl2 = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID)
textList2 = transformWindowData(wl2)

#verify the distinction
w = NSMutableSet.setWithArray_(textList1)
w.minusSet_(NSSet.setWithArray_(textList2))

#print the distinction
printBeautifully(w)

Producción:

all home windows:
    PID WinID  x,y,w,h                  [Title] SubTitle
------- -----  ---------------------    -------------------------------------------
    204     2 {0,0,1280,800         }   [Window Server] Desktop
    479    36 {0,0,1280,800         }   [Dock] Desktop Image - DefaultDesktop.jpg
    731  2893 {640,0,640,800        }   [Finder] /Customers/wolf/Downloads
    731   260 {-608,-1440,2560,1440 }   [Finder]
    731   259 {0,0,1280,800         }   [Finder]
   1301   321 {0,366,1280,411       }   [Audio Hijack] Software Audio
Transfer goal window
    PID WinID  x,y,w,h                  [Title] SubTitle
------- -----  ---------------------    -------------------------------------------
   1301   321 {0,366,1280,411       }   [Audio Hijack] Software Audio

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles