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 #!/usr/bin/env bash
set -u
screenshot_dir="$HOME/Pictures/Screenshots" screenshot_dir="$HOME/Pictures/Screenshots"
remote_target="mac-attio:~/screenshot.png" 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() { notify() {
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" \ DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" \
@@ -15,12 +18,13 @@ shopt -s nullglob
existing_files=("$screenshot_dir"/*.png) existing_files=("$screenshot_dir"/*.png)
existing_count=${#existing_files[@]} existing_count=${#existing_files[@]}
# Take screenshot # Take screenshot (no timeout here so interactive capture isn't canceled)
niri msg action screenshot niri msg action screenshot >/dev/null 2>&1
# Wait for new file (timeout in 0.1s intervals) # Wait for new file (timeout in 0.1s intervals)
deadline=$((timeout * 10)) deadline=$((file_timeout * 10))
count=0 count=0
files=("$screenshot_dir"/*.png)
while (( count < deadline )); do while (( count < deadline )); do
files=("$screenshot_dir"/*.png) files=("$screenshot_dir"/*.png)
@@ -37,12 +41,20 @@ if (( ${#files[@]} <= existing_count )); then
fi fi
# Get the new file (most recent) # 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 # Small delay to ensure file is fully written
sleep 0.1 sleep 0.1
# Upload # Upload with strict SSH options so it never blocks waiting for prompts
if scp -q "$latest_file" "$remote_target"; then 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" notify "Screenshot" "Uploaded to Mac"
else
notify "Screenshot" "Upload to Mac failed"
fi fi