Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Get macOS window title through python bindings instead of running AppleScript #40

Closed
wants to merge 8 commits into from
26 changes: 13 additions & 13 deletions aw_watcher_window/lib.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
from typing import Optional
from typing import Callable, Dict, Optional


def get_current_window_linux() -> Optional[dict]:
def get_current_window_linux() -> Dict[str, str]:
from . import xlib
window = xlib.get_current_window()

Expand All @@ -16,15 +16,15 @@ def get_current_window_linux() -> Optional[dict]:
return {"appname": cls, "title": name}


def get_current_window_macos() -> Optional[dict]:
def initialize_get_macos_window() -> Callable[[], Dict[str, str]]:
from . import macos
# The side effectful import breaks thigns here
app = macos.get_current_app()
print ("appname" + macos.get_app_name(app) + ", title" + macos.get_app_title(app))
return {"appname": macos.get_app_name(app), "title": macos.get_app_title(app)}
def get_current_window_macos() -> Dict[str, str]:
app = macos.get_current_app()
print ("appname" + macos.get_app_name(app) + ", title" + macos.get_app_title(app))
return {"appname": macos.get_app_name(app), "title": macos.get_app_title(app)}
return get_current_window_macos


def get_current_window_windows() -> Optional[dict]:
def get_current_window_windows() -> Dict[str, str]:
from . import windows
window_handle = windows.get_active_window_handle()
app = windows.get_app_name(window_handle)
Expand All @@ -38,12 +38,12 @@ def get_current_window_windows() -> Optional[dict]:
return {"appname": app, "title": title}


def get_current_window() -> Optional[dict]:
def get_current_window() -> Optional[Callable[[], Dict[str, str]]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to get_current_window_fn?

if sys.platform.startswith("linux"):
return get_current_window_linux()
return get_current_window_linux
elif sys.platform == "darwin":
return get_current_window_macos()
return initialize_get_macos_window()
elif sys.platform in ["win32", "cygwin"]:
return get_current_window_windows()
return get_current_window_windows
else:
raise Exception("Unknown platform: {}".format(sys.platform))
3 changes: 2 additions & 1 deletion aw_watcher_window/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ def parse_args(default_poll_time: float, default_exclude_title: bool):


def heartbeat_loop(client, bucket_id, poll_time, exclude_title=False):
current_window_fn = get_current_window()
while True:
if os.getppid() == 1:
logger.info("window-watcher stopped because parent process died")
break

try:
current_window = get_current_window()
current_window = current_window_fn()
logger.debug(current_window)
except Exception as e:
logger.error("Exception thrown while trying to get active window: {}".format(e))
Expand Down