#!/usr/bin/env bash # Click the target window, then alternate: # Turn1 = paste clipboard into that window, # Turn2 = picks up the new clipboard from that window and plays it as sound reply. Repeats until Ctrl-C. set -euo pipefail # Dependencies check (fail early) for cmd in xdotool xclip sha256sum notify-send; do command -v "$cmd" >/dev/null 2>&1 || { echo "Require $cmd"; exit 1; } done echo "Click the target window now..." WIN=$(xdotool selectwindow) || { echo "No window selected"; exit 1; } echo "Selected window id: $WIN" window_exists() { xdotool getwindowname "$WIN" >/dev/null 2>&1 } last_clipboard_hash="" get_clipboard_hash() { local data data="$(xclip -selection clipboard -o 2>/dev/null)" || data="" printf '%s' "$data" | sha256sum | awk '{print $1}' } # Initialize last hash so the first immediate read doesn't trigger an action. last_clipboard_hash="$(get_clipboard_hash)" # State: 1 => Turn1, 2 => Turn2 state=1 INTERVAL=2 echo "Watching clipboard for changes. Turn1=paste, Turn2=keystroke. Ctrl-C to stop." while true; do # Exit if target window closed if ! window_exists; then echo "Target window $WIN no longer exists. Exiting." exit 0 fi clipboard_hash="$(get_clipboard_hash)" if [ "$clipboard_hash" != "$last_clipboard_hash" ]; then # Only act if we've seen a prior value (prevents treating the initial read as a change) if [ -n "$last_clipboard_hash" ]; then if [ "$state" -eq 1 ]; then # Turn1: paste clipboard into the selected window (sends Ctrl+V) xdotool key --window "$WIN" --clearmodifiers ctrl+v content="$(xclip -selection clipboard -o 2>/dev/null)" notify-send "Multiturn" "Turn1: pasted clipboard into window $WIN with:\n\n$content" sleep 0.2 xdotool key --window "$WIN" --clearmodifiers KP_Enter state=2 else # Turn2: perform your special keystroke (no paste) flatpak run net.mkiol.SpeechNote --action start-reading-clipboard notify-send "Multiturn" "Turn2: sent special keystroke to window $WIN" state=1 fi fi last_clipboard_hash="$clipboard_hash" fi sleep "$INTERVAL" done