testsAndMisc/scripts/digital_wellbeing/youtube-music-wrapper.sh
Krzysztof kuhy Rudnicki 7925f0f1f9 Fix focus app detection to use window titles instead of process names
- Changed from pgrep -f (matches any process with 'code' in cmdline) to
  xdotool window detection (only matches visible windows)
- VS Code background services (code-tunnel, etc.) no longer trigger blocking
- Music is only blocked when VS Code window is actually open
- Split detection into FOCUS_APPS_WINDOWS and FOCUS_APPS_PROCESSES arrays
2025-12-07 16:01:14 +01:00

72 lines
1.5 KiB
Bash

#!/bin/bash
# YouTube Music Wrapper - Blocks launch when focus apps are running
# This replaces the actual youtube-music binary
set -euo pipefail
REAL_BINARY="/opt/YouTube Music/youtube-music.real"
LOG_FILE="${XDG_STATE_HOME:-$HOME/.local/state}/music-parallelism/music-parallelism.log"
log_message() {
local msg
msg="$(date '+%Y-%m-%d %H:%M:%S') - $1"
echo "$msg" >&2
echo "$msg" >>"$LOG_FILE" 2>/dev/null || true
}
# Focus apps - window titles to check (only visible windows count)
FOCUS_APPS_WINDOWS=(
"Visual Studio Code"
"VSCodium"
"Cursor"
"IntelliJ IDEA"
"PyCharm"
"WebStorm"
"CLion"
"Rider"
"Sublime Text"
"Blender"
"Godot"
"Unity"
"Unreal Editor"
)
# Focus apps - process patterns to check
FOCUS_APPS_PROCESSES=(
"steam_app_"
"gamescope"
)
# Check if any focus app is running (window-based detection)
is_focus_app_running() {
# Check windows first
if command -v xdotool &>/dev/null; then
for app in "${FOCUS_APPS_WINDOWS[@]}"; do
if xdotool search --name "$app" &>/dev/null 2>&1; then
echo "$app"
return 0
fi
done
fi
# Check specific processes
for app in "${FOCUS_APPS_PROCESSES[@]}"; do
if pgrep -f "$app" &>/dev/null; then
echo "$app"
return 0
fi
done
return 1
}
# Main
if focus_app=$(is_focus_app_running); then
log_message "BLOCKED: YouTube Music launch prevented (focus app: $focus_app)"
notify-send -u normal -t 3000 "🚫 YouTube Music Blocked" "Focus mode active ($focus_app)" 2>/dev/null || true
exit 1
fi
# No focus app running, launch normally
exec "$REAL_BINARY" "$@"