fix for negative pid or race conditions
This commit is contained in:
parent
d4ff50ee14
commit
9e35523c78
@ -29,9 +29,11 @@ def timestamp_to_human_readable(timestamp):
|
|||||||
def get_active_app_name_osx():
|
def get_active_app_name_osx():
|
||||||
from AppKit import NSWorkspace
|
from AppKit import NSWorkspace
|
||||||
|
|
||||||
active_app = NSWorkspace.sharedWorkspace().activeApplication()
|
try:
|
||||||
return active_app["NSApplicationName"]
|
active_app = NSWorkspace.sharedWorkspace().activeApplication()
|
||||||
|
return active_app["NSApplicationName"]
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
|
||||||
def get_active_window_title_osx():
|
def get_active_window_title_osx():
|
||||||
from Quartz import (
|
from Quartz import (
|
||||||
@ -40,14 +42,17 @@ def get_active_window_title_osx():
|
|||||||
kCGWindowListOptionOnScreenOnly,
|
kCGWindowListOptionOnScreenOnly,
|
||||||
)
|
)
|
||||||
|
|
||||||
app_name = get_active_app_name_osx()
|
try:
|
||||||
windows = CGWindowListCopyWindowInfo(
|
app_name = get_active_app_name_osx()
|
||||||
kCGWindowListOptionOnScreenOnly, kCGNullWindowID
|
windows = CGWindowListCopyWindowInfo(
|
||||||
)
|
kCGWindowListOptionOnScreenOnly, kCGNullWindowID
|
||||||
for window in windows:
|
)
|
||||||
if window["kCGWindowOwnerName"] == app_name:
|
for window in windows:
|
||||||
return window.get("kCGWindowName", "Unknown")
|
if window["kCGWindowOwnerName"] == app_name:
|
||||||
return None
|
return window.get("kCGWindowName", "Unknown")
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_active_app_name_windows():
|
def get_active_app_name_windows():
|
||||||
@ -55,17 +60,23 @@ def get_active_app_name_windows():
|
|||||||
import win32gui
|
import win32gui
|
||||||
import win32process
|
import win32process
|
||||||
|
|
||||||
hwnd = win32gui.GetForegroundWindow()
|
try:
|
||||||
_, pid = win32process.GetWindowThreadProcessId(hwnd)
|
hwnd = win32gui.GetForegroundWindow()
|
||||||
exe = psutil.Process(pid).name()
|
_, pid = win32process.GetWindowThreadProcessId(hwnd)
|
||||||
return exe
|
exe = psutil.Process(pid).name()
|
||||||
|
return exe
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_active_window_title_windows():
|
def get_active_window_title_windows():
|
||||||
import win32gui
|
import win32gui
|
||||||
|
|
||||||
hwnd = win32gui.GetForegroundWindow()
|
try:
|
||||||
return win32gui.GetWindowText(hwnd)
|
hwnd = win32gui.GetForegroundWindow()
|
||||||
|
return win32gui.GetWindowText(hwnd)
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_active_app_name_linux():
|
def get_active_app_name_linux():
|
||||||
|
Loading…
Reference in New Issue
Block a user