-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_active_window_macos.py
43 lines (36 loc) · 1.31 KB
/
get_active_window_macos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
This module provides functionality to get the currently active window's owner name and title on macOS.
Classes:
AppListener: A class to listen for active window changes and retrieve the active window's details.
Functions:
get_active_window: Get the currently active window's owner name and title.
"""
import objc
from Foundation import NSObject, NSNotificationCenter
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)
class AppListener(NSObject):
def __init__(self):
"""
Initialize the AppListener and set up notifications.
"""
objc.super(AppListener, self).init()
if self is None:
return
self.setup_notifications()
def get_active_window(self):
"""
Get the currently active window's owner name and title.
Returns:
tuple: A tuple containing the owner name and window title.
"""
options = kCGWindowListOptionOnScreenOnly
window_list = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
for window in window_list:
if window.get('kCGWindowLayer') == 0: # Standard windows have layer 0
owner_name = window.get('kCGWindowOwnerName', 'Unknown')
return owner_name
return None, None