Files
rune-docker/window-title-sync.py
Seth Van Niekerk e36e9bc4b5
All checks were successful
Build & Push Docker Image / build-and-publish (push) Successful in 56s
Test Notifications & Title Updates
2026-02-05 16:24:18 -05:00

94 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""
Window title synchronization for Selkies
Monitors the active window title and updates the browser tab title dynamically
Note: This script is designed to run inside a Linux container with X11 tools installed.
Pylance warnings about missing imports are expected in the development environment.
"""
import subprocess
import time
import os
import sys
def get_active_window_title():
"""Get the title of the currently active window"""
try:
# Try using xdotool first
result = subprocess.run(
['xdotool', 'getactivewindow', 'getwindowname'],
capture_output=True,
text=True,
timeout=1
)
if result.returncode == 0:
return result.stdout.strip()
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
try:
# Fallback to wmctrl
result = subprocess.run(
['wmctrl', '-l'],
capture_output=True,
text=True,
timeout=1
)
if result.returncode == 0:
lines = result.stdout.strip().split('\n')
for line in lines:
if 'RuneLite' in line:
# Extract title (after the third column)
parts = line.split(None, 3)
if len(parts) >= 4:
return parts[3]
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
return None
def update_browser_title(title):
"""Update the Selkies browser tab title"""
title_file = '/opt/gst-web/title'
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(title_file), exist_ok=True)
try:
with open(title_file, 'w') as f:
f.write(title)
except Exception as e:
print(f"Error updating title file: {e}", file=sys.stderr)
def main():
print("Window title sync started - monitoring RuneLite window")
last_title = None
# Initial delay to let X11 and windows initialize
time.sleep(5)
while True:
try:
current_title = get_active_window_title()
# Only update if title has changed and contains "RuneLite"
if current_title and 'RuneLite' in current_title and current_title != last_title:
print(f"Window title changed to: {current_title}")
update_browser_title(current_title)
last_title = current_title
# Check every 2 seconds
time.sleep(2)
except KeyboardInterrupt:
print("\nWindow title sync stopped")
break
except Exception as e:
print(f"Error in title monitoring: {e}", file=sys.stderr)
time.sleep(5)
if __name__ == "__main__":
main()