From 134d9775d6e330e5c6d46ff1f9fe23c18485a0c5 Mon Sep 17 00:00:00 2001 From: iglhaut <67795031+iglhaut@users.noreply.github.com> Date: Sun, 27 Apr 2025 17:00:03 +0100 Subject: [PATCH] Restore focus to editor after sending do-file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Saves the previously active window ID, activates Stata to ensure it receives keystrokes, then restores focus to the editor so your workflow isn’t interrupted. --- sublime-stata.sh | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/sublime-stata.sh b/sublime-stata.sh index df22ad9..207937c 100755 --- a/sublime-stata.sh +++ b/sublime-stata.sh @@ -1,20 +1,30 @@ #!/bin/bash # Dependencies: xdotool -# Copy 'do ' to clipboard -string='do ''"'$1'"' +# Build the do-file command +string="do \"$1\"" -# Get Stata window ID (first window that matches the regex) +# Find Stata winid_stata=$(xdotool search --name --limit 1 "Stata/(IC|SE|MP)? 1[0-9]\.[0-9]") -# Send string to Stata's command pane, selecting previous text and copying on top -# Note: there is an issue with xdotool's clearmodifiers option (see https://github.com/jordansissel/xdotool/issues/43) -# Note: Should there be some issues, try adjusting the --delay flag. -if [ ! -z "$winid_stata" ]; then - xdotool key --window ${winid_stata} --delay 10 ctrl+a - xdotool type --window ${winid_stata} --delay 1 "${string}" - xdotool key --window ${winid_stata} Return -else - echo "No Stata window open." - exit 1 -fi \ No newline at end of file +if [ -z "$winid_stata" ]; then + echo "No Stata window open." + exit 1 +fi + +# 1) Save the ID of whatever window is currently active +winid_before=$(xdotool getactivewindow) + +# 2) Bring Stata forward, wait to settle +xdotool windowactivate $winid_stata +sleep 0.1 + +# 3) Send the do-file command +xdotool key ctrl+a +sleep 0.05 +xdotool type --delay 1 "$string" +sleep 0.05 +xdotool key Return + +# 4) Restore focus to your original window +xdotool windowactivate $winid_before