improve screenshot script

This commit is contained in:
2026-02-24 20:17:33 +00:00
parent c44a85e404
commit 6978c77066
2 changed files with 32 additions and 13 deletions
+1 -1
View File
@@ -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; }
+31 -12
View File
@@ -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