From 6978c770660f9c9ce098ccd2e0cc221f9fdbab14 Mon Sep 17 00:00:00 2001 From: "Thomas G. Lopes" Date: Tue, 24 Feb 2026 20:17:33 +0000 Subject: [PATCH] improve screenshot script --- niri/files/config.kdl | 2 +- niri/files/copy-latest-screenshot.sh | 43 ++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/niri/files/config.kdl b/niri/files/config.kdl index 947fa0b..fa84c1c 100644 --- a/niri/files/config.kdl +++ b/niri/files/config.kdl @@ -489,7 +489,7 @@ binds { // Mod+Space { switch-layout "next"; } // Mod+Shift+Space { switch-layout "prev"; } - Mod+P { spawn "sh" "-c" "niri msg action screenshot && ~/.config/niri/copy-latest-screenshot.sh"; } + Mod+P { spawn "~/.config/niri/copy-latest-screenshot.sh"; } Mod+Ctrl+P { screenshot-screen; } Mod+Alt+P { screenshot-window; } diff --git a/niri/files/copy-latest-screenshot.sh b/niri/files/copy-latest-screenshot.sh index c2b1dd9..b577380 100755 --- a/niri/files/copy-latest-screenshot.sh +++ b/niri/files/copy-latest-screenshot.sh @@ -2,6 +2,7 @@ screenshot_dir="$HOME/Pictures/Screenshots" remote_target="mac-attio:~/screenshot.png" +timeout=3 # seconds notify() { DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" \ @@ -9,21 +10,39 @@ notify() { notify-send "$@" } +# Record existing files shopt -s nullglob -files=("$screenshot_dir"/*.png) -latest_file="" -if (( ${#files[@]} )); then - latest_file=$(ls -1t "${files[@]}" | head -n 1) +existing_files=("$screenshot_dir"/*.png) +existing_count=${#existing_files[@]} + +# Take screenshot +niri msg action screenshot + +# Wait for new file (timeout in 0.1s intervals) +deadline=$((timeout * 10)) +count=0 + +while (( count < deadline )); do + files=("$screenshot_dir"/*.png) + if (( ${#files[@]} > existing_count )); then + break + fi + sleep 0.1 + ((count++)) +done + +# Check if a new file appeared +if (( ${#files[@]} <= existing_count )); then + exit 0 # Canceled or failed, silent exit fi -if [[ -z "${latest_file:-}" ]]; then - notify "Screenshot upload" "No screenshots found in $screenshot_dir" - exit 1 -fi +# Get the new file (most recent) +latest_file=$(ls -1t "${files[@]}" | head -n 1) +# Small delay to ensure file is fully written +sleep 0.1 + +# Upload if scp -q "$latest_file" "$remote_target"; then - notify "Screenshot upload" "Uploaded $(basename "$latest_file")" -else - notify "Screenshot upload" "Upload failed" - exit 1 + notify "Screenshot" "Uploaded to Mac" fi