fix screenshots

This commit is contained in:
2026-04-13 09:57:47 +01:00
parent 6003f41a12
commit 4af7031922
+19 -7
View File
@@ -1,8 +1,11 @@
#!/usr/bin/env bash
set -u
screenshot_dir="$HOME/Pictures/Screenshots"
remote_target="mac-attio:~/screenshot.png"
timeout=3 # seconds
file_timeout=8 # seconds to wait for screenshot file to appear
upload_timeout=10 # seconds
notify() {
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" \
@@ -15,12 +18,13 @@ shopt -s nullglob
existing_files=("$screenshot_dir"/*.png)
existing_count=${#existing_files[@]}
# Take screenshot
niri msg action screenshot
# Take screenshot (no timeout here so interactive capture isn't canceled)
niri msg action screenshot >/dev/null 2>&1
# Wait for new file (timeout in 0.1s intervals)
deadline=$((timeout * 10))
deadline=$((file_timeout * 10))
count=0
files=("$screenshot_dir"/*.png)
while (( count < deadline )); do
files=("$screenshot_dir"/*.png)
@@ -37,12 +41,20 @@ if (( ${#files[@]} <= existing_count )); then
fi
# Get the new file (most recent)
latest_file=$(ls -1t "${files[@]}" | head -n 1)
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
# Upload with strict SSH options so it never blocks waiting for prompts
if timeout "${upload_timeout}s" scp -q \
-o BatchMode=yes \
-o ConnectTimeout=5 \
-o ConnectionAttempts=1 \
-o ServerAliveInterval=2 \
-o ServerAliveCountMax=1 \
-- "$latest_file" "$remote_target"; then
notify "Screenshot" "Uploaded to Mac"
else
notify "Screenshot" "Upload to Mac failed"
fi